Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

This question already has an answer here:

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

marked as duplicate by Levi Morrison, PeeHaa Aug 28 '14 at 16:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

up vote 11 down vote accepted

Use implode function ie

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

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