[ACCEPTED]-PDO with "WHERE... IN" queries-pdo

Accepted answer
Score: 36

Since you can't mix Values (the Numbers) with 3 control flow logic (the commas) with prepared 2 statements you need one placeholder per 1 Value.

$idlist = array('260','201','221','216','217','169','210','212','213');

$questionmarks = str_repeat("?,", count($idlist)-1) . "?";

$stmt = $dbh->prepare("DELETE FROM `foo` WHERE `id` IN ($questionmarks)");

and loop to bind the parameters.

Score: 8

This may be helpful too:

https://phpdelusions.net/pdo#in

$arr = [1,2,3];
$in  = str_repeat('?,', count($arr) - 1) . '?';
$sql = "SELECT * FROM table WHERE column IN ($in)";
$stm = $db->prepare($sql);
$stm->execute($arr);
$data = $stm->fetchAll();

0

More Related questions