您不能将两个变量与一个绑定question mark
!
对于您绑定的每个变量,您都需要一个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
到达。
寻找nick9v
mysqli-stmt.bind-param的解决方案