Mysql where id is in array [duplicate] Mysql where id is in array [duplicate] arrays arrays

Mysql where id is in array [duplicate]


$string="1,2,3,4,5";$array=array_map('intval', explode(',', $string));$array = implode("','",$array);$query=mysqli_query($conn, "SELECT name FROM users WHERE id IN ('".$array."')");

NB: the syntax is:

SELECT * FROM table WHERE column IN('value1','value2','value3')


Your query translates to:

SELECT name FROM users WHERE id IN ('Array');

Or something to that affect.

Try using prepared queries instead, something like:

$numbers = explode(',', $string);$prepare = array_map(function(){ return '?'; }, $numbers);$statement = mysqli_prepare($link , "SELECT name FROM users WHERE id IN ('".implode(',', $prepare)."')");if($statement) {   $ints = array_map(function(){ return 'i'; }, $numbers);   call_user_func_array("mysqli_stmt_bind_param", array_merge(      array($statement, implode('', $ints)), $numbers   ));   $results = mysqli_stmt_execute($statement);   // do something with results    // ...}


Change

$array=array_map('intval', explode(',', $string));

To:

$array= implode(',', array_map('intval', explode(',', $string)));

array_map returns an array, not a string. You need to convert the array to a comma separated string in order to use in the WHERE clause.