【问题标题】:PDO: Bind an array to an IN() condition with multiple WHERE clauses?PDO:将数组绑定到具有多个 WHERE 子句的 IN() 条件?
【发布时间】:2017-11-18 16:26:43
【问题描述】:

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

请指教。

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-01-06
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      相关资源
      最近更新 更多