在单个查询pdo中绑定IN()和其他变量

时间:2016-09-08 15:51:03

标签: arrays pdo binding

我使用 this method来绑定IN()条件,但我的问题仍然是

$in  = str_repeat('?,', count($in_array) - 1) . '?';
$sql = "SELECT * FROM my_table WHERE my_value IN ($in) and other_value='$variable' ";
$stm = $db->prepare($sql);
$stm->execute($in_array);

如何在此查询中绑定 $ variable

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

简单,构建一个占位符数组,为额外的$variable添加额外的占位符,然后将$variable的值添加到您传递到execute()调用的数组中:

$temp = array_fill('?', count($array_in) - 1);
$placeholders = implode(',', $temp);

$array_in[] = $variable; // add your $variable to the values list

$sql = "SELECT ... IN ($placeholders) AND other_value = ?"; // insert placeholders string into query
$stmt = $dbh->prepare($sql);
$stmt->execute($array_in);