我在此代码中使用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()的错误:
在布尔值
上调用成员函数fetch_array()
但是,如果我在代码中创建数组,它可以正常工作。例如:
...
call_user_func_array(array($stmt,"bind_param"),array('ii',1,1));
...
答案 0 :(得分:0)
如果当然知道涉及哪种查询会很好,但无论如何以下代码都有效:
<?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 Stackoverflow的引用。