Jump to content

Using an array in MySQL where id IN ($array)?


extrovertive

Recommended Posts

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 usertable
WHERE 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?
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.