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

PDO: Bind an array to an IN() condition with multiple WHERE clauses?

基于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));

请指教

execute 接受单个参数数组。你需要做的:

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