Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).")";

You can write your query like this:

select * from table where id in (1, 4, 6, 7)

You can add as many values as you need.