MySQL PHP - SELECT WHERE id = array()? [duplicate] MySQL PHP - SELECT WHERE id = array()? [duplicate] arrays arrays

MySQL PHP - SELECT WHERE id = array()? [duplicate]


Use IN.

$sql = 'SELECT *           FROM `table`          WHERE `id` IN (' . implode(',', array_map('intval', $array)) . ')';


What you are looking for is the IN() statement. This will test if a given field contains any of 1 or more values.

SELECT * FROM `Table` WHERE `id` IN (1, 2, 3)

You can use a simple loop to convert your $array variable into the proper format. Be sure to be mindful of SQL injection if your array values are coming from the front end.


Simply use the ID IN () syntax:

$sql = "SELECT FROM `table` WHERE `ID` IN (".implode(',',$array).")";