SELECTING WHERE ID from array in a php-mysql query

Discuss coding issues, and scripts related to PHP and MySQL.
Marius
Posts:107

SELECTING WHERE ID from array in a php-mysql query

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 ...

Admin Posts:805
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)";