你不能用一个问号
绑定两个变量!
对于绑定的每个变量,您都需要一个question mark
“bind_param”检查每个变量是否与要求匹配。之后,字符串值放在引号之间。
这将不起作用。
"SELECT name FROM table WHERE city IN (?)"; ( becomes too )
$q_prepared->bind_param("s", $cities);
"SELECT name FROM table WHERE city IN ('city1,city2,city3,city4')";
一定是。
"SELECT name FROM table WHERE city IN (?,?,?,?)"; ( becomes too )
$q_prepared->bind_param("ssss", $city1,$city2,$city3,$city4);
"SELECT name FROM table WHERE city IN ('city1','city2','city3','city4')";
$query_prepared->bind_param
逐个引用字符串参数。
变量的数量和字符串类型的长度必须与语句中的参数匹配。
$query_str= "SELECT name FROM table WHERE city IN ('Nashville','Knoxville')";
将成为
$query_str= "SELECT name FROM table WHERE city IN (?,?)";
现在一定是bind_param
bind_param("ss",$arg1,$arg2)
有了这个
$query_str= "SELECT name FROM table WHERE city IN (?)";
和bind_param
bind_param("s",$cities)
你得到
$query_str= "SELECT name FROM table WHERE city IN ('Nashville,Knoxville')";
这就是为什么数组不起作用的原因。
这个事实的唯一解决方案是call_user_func_array
如果您初始化语句,则不需要以下操作
$query_prepared = $mysqli->stmt_init();
if($query_prepared && $query_prepared->prepare($query_str)) {
这是正确的
$query_prepared = $mysqli->stmt_init();
if($query_prepared->prepare($query_str)) {
如果您不想使用
并且只有少量参数,
则可以使用以下代码进行操作。call_user_func_array
[...]
$cities= explode(",", $_GET['cities']);
if (count($cities)>3) { echo "too many arguments"; }
else
{
$count = count($cities);
$SetIn = "(";
for($i = 0; $i < $count; ++$i) {
$code.='s';
if ($i>0) {$SetIn.=",?";} else {$SetIn.="?";}
}
$SetIn.=")";
$query_str= "SELECT name FROM table WHERE city IN ".$SetIn;
// with 2 arguments $query_str will look like
// SELECT name FROM table WHERE city IN (?,?)
$query_prepared = $mysqli->stmt_init();
if($query_prepared->prepare($query_str))
{
if ($count==1) { $query_prepared->bind_param($code, $cities[0]);}
if ($count==2) { $query_prepared->bind_param($code, $cities[0],$cities[1]);}
if ($count==3) { $query_prepared->bind_param($code, $cities[0],$cities[1],$cities[2]);
// with 2 arguments $query_prepared->bind_param() will look like
// $query_prepared->bind_param("ss",$cities[0],$cities[1])
}
$query_prepared->execute();
}
[...]
}
我建议你尝试一下。call_user_func_array
寻找
mysqli-stmt.bind-param 的解决方案nick9v