4
votes

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?

1

1 Answers

16
votes

Use implode function ie

$QueryStr = "SELECT * FROM users WHERE userID IN (".implode(',', $userIDs).")";