PDO:将数组绑定到具有多个WHERE子句的IN()条件?

时间:2017-06-16 02:47:05

标签: php arrays pdo prepared-statement where-in

基于Can I bind an array to an IN() condition?

上述问题对使用多个WHERE子句没有任何答案。

第二个最佳答案仅适用于一个WHERE子句:

$qMarks = str_repeat('?,', count($ids) - 1) . '?';
$sth = $db->prepare("SELECT * FROM myTable WHERE id IN ($qMarks)");
$sth->execute($ids);

如果我添加了额外的WHERE子句,则会产生空结果或错误。

$qMarks = str_repeat('?,', count($ids) - 1) . '?';
$sth = $db->prepare("SELECT * FROM myTable WHERE id IN ($qMarks) AND locationid=?");
$sth->execute(array($ids, ?)); // Also tried $sth->execute($ids, $location_id) and $sth->execute($ids, array($location_id));

请告知。

1 个答案:

答案 0 :(得分:1)

execute接受一个参数数组。你需要这样做:

$sth->execute(array_merge($ids, [$location_id]));