Forum Moderators: coopster

Message Too Old, No Replies

searching a mysql column against multiple variables

mysql single column multiple variables

         

ryanf

2:34 am on Sep 17, 2007 (gmt 0)

10+ Year Member



I have an array of zipcodes which I am trying to check against a mysql table. I want to search in the column "zipcode" in a table of a bunch of entries. I want to search each rown to see if the value of the zipcode column is listed within my array.

This could be said vis vera, that I want to check the mysql table to see if any of the rows have one of the values from the array inside the zipcode column.

I've tried the statement

"select * from table where zipcode IN ('$zipsearch')", but it seems to return an odd sequence of data. It doesn't seem thurow enough.

similarly the entire statement is :
select * from table where description like \"%$trimmed%\" OR title like \"%$trimmed%\" AND zipcode IN ('$zipsearch') order by date"

I can't seem to get the statemtent to break up my requests right so that the logistics of teh statement are :

select from table where (descriptions like variable or title like variable) AND <zipcode search problem>

Any help?

thanks much!

omoutop

6:06 am on Sep 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



when you use the IN statement, your data myst be in the following format

.... where fields IN (3,4,5,6,7,8,0,33,56,89)

you said you have an array holding your zipcodes.
have you remembered transforming it with implode(",", $my_array) before using it in your sql?

ryanf

6:18 am on Sep 17, 2007 (gmt 0)

10+ Year Member



ohh, i forgot to mention that it's already in that format. The query does work, but it seems to search in some odd order. It will only pull a certain zipcode once, or may not select on at all that's in the list.
It seems to be a formating issue. I even tried to sort the array numerically and no luck

so, the array is in order and seperated by ","

Good guess though

rjwmnews

3:05 pm on Sep 17, 2007 (gmt 0)



My experience indicates that PHP likes to treat arrays with string numbers as numbers (maybe any string number variable) due to lack of strong variable typing. The problem is likely due to zip codes that begin with zero, leading zeroes being truncated. Try quoting all zip codes in the delimited string for the 'WHERE IN()' SQL.

$zip_list = implode("','", $zips_array);
$zip_list = "'" . $zip_list . "'";
WHERE SearchZipCode IN('.$zip_list.')

jatar_k

3:14 pm on Sep 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you need to take php out of the equation and test it from the commandline or in something like phpmyadmin and see what the query returns

you can then tweak the query there before you go back to getting it working with php

ryanf

8:53 pm on Sep 17, 2007 (gmt 0)

10+ Year Member



ok, figured it out. it was a problem with the mysql syntax

i was searching "sql= select variable in table where zipcode in ('zip1, zip2')

but it should have been ('zip1','zip2') <- need the apostrophes

thanks for all your help!