库MySQLi绑定参数与在一个阵列 [英] MySQLi Bind Param with an array for IN
问题描述
我想传递一个数组 $ stmt-> bind_param
为作为一个变量。我怎样才能做到这一点?
I am trying to pass an array to $stmt->bind_param
for as an IN variable. How can I do this?
$values = array('a','b','c','d');
$values = '"' . implode('","', $values) . '"';
$stmt->prepare('SELECT value1, value2 FROM table1 WHERE value3 IN (?)');
$stmt->bind_param('s', $values);
我不能让它为我的生活工作。有什么想法吗?以上code是只是一个样本。
I can't get it to work for the life of me. Any thoughts? The above code is just a sample.
推荐答案
这是一种情况,做这种方式是不合适的。你构建实际的SQL(这是逗号和引号),并把它当作一个参数。它基本上是要评估 VALUE3 IN('...')
,其中 ...
是整体 $值
。
This is a scenario where doing it this way is inappropriate. You're constructing actual SQL (that's what the commas and quotes are), and passing it in as a parameter. It's basically evaluating to value3 IN ('...')
where ...
is the entirety of $values
.
另外那对引号中的良好的通话。 MySQL使用单引号。
Also that's a good call about the quotes. MySQL uses single quotes.
您将需要单独使用字符串连接来完成构建的SQL,或者使用多个参数。
You'll need to either build the SQL using string concatenation alone, or use more than one parameter.
修改
作为一个例子:
$values = array('a','b','c','d');
$values = "'" . implode("','", $values) . "'";
$stmt->prepare('SELECT value1, value2 FROM table1 WHERE value3 IN (' . $values . ')');
这篇关于库MySQLi绑定参数与在一个阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!