Page 1 of 1

SELECTING WHERE ID from array in a php-mysql query

Posted: 14 Dec 2014, 08:11
by Marius
I'm trying to SELECT from a PHP array inside a mysql query.

Code: Select all

$sql = "SELECT * FROM table WHERE id = $ids ";
$ids has been created by $_POST and exploded into an array. It can be [ 2 ] or [2,4,12] for example. Not sure whether to implode back into string or not.
I need to be selecting the rows with id 2 OR 4 OR 12 ...

SELECTING WHERE ID from array in a php-mysql query

Posted: 14 Dec 2014, 08:23
by Admin
Try with IN with implode of the array $id like:

Code: Select all

$sql = "SELECT * FROM table WHERE id IN (". implode(',', $ids) .")";
- Make sure that $ids is an array.
Suppose your

Code: Select all

$ids = array(2, 4, 12);
Then sql will be :

Code: Select all

$sql = "SELECT * FROM table WHERE id IN (2,4,12)";