0

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!

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can't emphasize the "make sure you're sanitizing the input" part enough.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.