views:

286

answers:

1

Hello,

I'm currently using $.getJSON to pass an array of ids. It basically constructs a URL like this:

 http://domain.com/json.php?id=1&id=2&id=4

My question is: How can I take these ids that are passed (1,2,4) and place them in my where clause?

Something like:

$id = $_GET['id'];

 $sql = SELECT * FROM table WHERE usrID IN ($id);

Thanks very much!

+2  A: 

Just join/implode them into a single string seperated by commas.

$sql = 'SELECT * FROM table WHERE usrID IN (' . join(',', $id) . ');';

You'll also want to make sure you're sanitizing the input.

fiXedd
Can't emphasize the "make sure you're sanitizing the input" part enough.
KiNgMaR
Good ol' Bobby Tables... http://xkcd.com/327/
fiXedd
Voted up. Just to add join will also work just fine for arrays of zero or one elements; i.e. the resulting string from joining an array with just one element will not get a ',' appended to it (so it's still okay in your IN(..). An array with zero elements will return the empty string, you might want to test for that before constructing your query.
karim79