我使用 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 ?
任何人都可以帮忙吗?
答案 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);