Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to make sql query for rows matching values in a php array.

Essentially I have an array like

$userIDs[0] = 23456;
$userIDs[1] = 42901;
$userIDs[2] = 82731;
$userIDs[3] = 23921;

And want to perform a single SQL query to get rows matching this array

SELECT * FROM users WHERE userID IN $userIDs

Is there an easy way to do this? Or do I have to manually construct the query string?

share|improve this question
possible duplicate of PHP/MYSQL using an array in WHERE clause – mario Sep 4 '11 at 9:44

1 Answer

up vote 9 down vote accepted

Use implode function ie

$QueryStr = "SELECT * FROM users WHERE userID IN (".implode(',', $userIDs).")";
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.