extrovertive Posted July 17, 2006 Share Posted July 17, 2006 I have an array - $zips = array('92120', '92127')$zips actually contain a hundred more is generated dynamically.Now, can I use this kind of SQL?SELECT username, zip FROM usertableWHERE zip IN ('$zips')I tried but it doesn't work. Basically, $zips is an array that contain a lot of zip codes from a class I'm using. However, I can't use an array in a "IN" statement for MySQL? Link to comment Share on other sites More sharing options...
hvle Posted July 17, 2006 Share Posted July 17, 2006 no you can, you have to separate all zipcodes into a string:SELECT username, zip FROM usertable WHERE zip IN ('92120', '92127');it's easy to write a snippet of code to convert your array into a string like that. Link to comment Share on other sites More sharing options...
akabugeyes Posted July 17, 2006 Share Posted July 17, 2006 How about trying FIND_IN_SET?SELECT username, zip FROM usertableWHERE FIND_IN_SET (zip,'$zips'); Link to comment Share on other sites More sharing options...
Joe Haley Posted July 17, 2006 Share Posted July 17, 2006 http://ca3.php.net/implodeimplode(); is your friend. Link to comment Share on other sites More sharing options...
extrovertive Posted July 17, 2006 Author Share Posted July 17, 2006 I'm trying implode: $zips = array('92120', '92127'); $zips = implode(',',$zips); SELECT username, zip FROM usertable WHERE zip IN ($zips);yields no result, yet just using WHERE zip IN ('92120', '92127') does. I have also implode it using '', ',' but still doesnt' work. FIND_IN_SET give the same result. Link to comment Share on other sites More sharing options...
extrovertive Posted July 17, 2006 Author Share Posted July 17, 2006 Ah, here's the solution.$zips = array(92102, 92127, 92103);$quoted_zips= array(); foreach ($zips AS $v) { $quoted_zips[]= "'{$v}'"; } $zip_list= implode(',', $quoted_zips); SELECT username, zip FROM usertableWHERE zip IN ($zips) Link to comment Share on other sites More sharing options...
Recommended Posts