call_user_func_array "doesn't work" 和 bind_param 当传递已经在变量中的数组时
call_user_func_array "doesn't work" with bind_param when passing array already in variable
我在这段代码中使用 call_user_func_array 时遇到问题
$stmt=$mysqli->prepare($query);
$txt=array('ii',1,1);
call_user_func_array(array($stmt,"bind_param"),$txt);
$stmt->execute();
$result=$stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_ASSOC)){
$r[$count]=$row;
$count++;
}
使用 $txt 变量给出错误 fetch_array():
Call to a member function fetch_array() on boolean
但是,如果我在代码中创建数组,它就可以正常工作。示例:
...
call_user_func_array(array($stmt,"bind_param"),array('ii',1,1));
...
如果知道涉及哪种查询当然会很高兴,但无论如何以下代码都有效:
<?php
error_reporting(E_ALL);
$id=5;
$code = 'GR';
$country='Greece';
$mysqli = new mysqli("localhost", "root", "", "exp");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " .
$mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
if (!($stmt = $mysqli->prepare("INSERT INTO countries VALUES (?,?,?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
//$stmt->bind_param('iss', $id, $code, $country);
$params = array('iss',$id,$code,$country);
$tmp = array();
foreach($params as $key => $value) $tmp[$key] = &$params[$key];
call_user_func_array( array($stmt, 'bind_param'), $tmp);
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
只有当 $params 数组包含指定数据类型的字母时,该代码才适用于我的软件。参数本身也需要是引用而不是值。注意:我确实借用了将值转换为来自 this member of Whosebug.
的引用的代码
我在这段代码中使用 call_user_func_array 时遇到问题
$stmt=$mysqli->prepare($query);
$txt=array('ii',1,1);
call_user_func_array(array($stmt,"bind_param"),$txt);
$stmt->execute();
$result=$stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_ASSOC)){
$r[$count]=$row;
$count++;
}
使用 $txt 变量给出错误 fetch_array():
Call to a member function fetch_array() on boolean
但是,如果我在代码中创建数组,它就可以正常工作。示例:
...
call_user_func_array(array($stmt,"bind_param"),array('ii',1,1));
...
如果知道涉及哪种查询当然会很高兴,但无论如何以下代码都有效:
<?php
error_reporting(E_ALL);
$id=5;
$code = 'GR';
$country='Greece';
$mysqli = new mysqli("localhost", "root", "", "exp");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " .
$mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
if (!($stmt = $mysqli->prepare("INSERT INTO countries VALUES (?,?,?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
//$stmt->bind_param('iss', $id, $code, $country);
$params = array('iss',$id,$code,$country);
$tmp = array();
foreach($params as $key => $value) $tmp[$key] = &$params[$key];
call_user_func_array( array($stmt, 'bind_param'), $tmp);
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
只有当 $params 数组包含指定数据类型的字母时,该代码才适用于我的软件。参数本身也需要是引用而不是值。注意:我确实借用了将值转换为来自 this member of Whosebug.
的引用的代码