简体   繁体   English

当在变量中传递数组时,call_user_func_array与bind_param一起“不起作用”

[英]call_user_func_array “doesn't work” with bind_param when passing array already in variable

i'm having trouble using call_user_func_array in this code 我在此代码中使用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++;
}

Using the $txt variable gives an error with fetch_array(): 使用$ txt变量会导致fetch_array()错误:

Call to a member function fetch_array() on boolean 在布尔值上调用成员函数fetch_array()

But, if i create the array in the code it works fine. 但是,如果我在代码中创建数组,则效果很好。 Example: 例:

...
call_user_func_array(array($stmt,"bind_param"),array('ii',1,1));
...

If course it would be nice to know what kind of a query is involved, but in any event the following code works: 如果可以的话,很高兴知道涉及哪种查询,但是无论如何以下代码都可以工作:

<?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;
}

The code only works with my software when the $params array includes the letters specifying the type of data. 该代码仅在$ params数组包含指定数据类型的字母时与我的软件一起使用。 Also the params themselves need to be references and not values. 同样,参数本身也需要引用而不是值。 Note: I did borrow the code to turn values into references from this member of Stackoverflow . 注意:我确实借用了代码,将值转换为来自Stackoverflow的该成员的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP使用call_user_func_array来bind_param - PHP Using call_user_func_array to bind_param &amp; 符号和 call_user_func_array(array($stmt, &quot;bind_param&quot;), $value) 在 php 中做什么? - What do & symbol and call_user_func_array(array($stmt, "bind_param"), $value) do in php? call_user_func_array()-警告:mysqli_stmt :: bind_param():变量数与准备好的语句中的参数数不匹配 - call_user_func_array() - Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement php call_user_func_array不起作用 - php call_user_func_array doesn't work PHP 5.5,无法使call_user_func_array与bing_param一起使用 - PHP 5.5, can't get call_user_func_array to work with bing_param 使用带有可变参数的call_user_func_array传递引用 - Passing a reference using call_user_func_array with variable arguments call_user_func_array,带有变量名 - call_user_func_array with variable name 何时使用 call_user_func_array - When to use call_user_func_array 我们可以通过引用将参数数组传递给call_user_func_array()而不是传递变量引用数组吗? - Can we pass the parameter array by reference to call_user_func_array() instead of passing an array of variable references? 将参数作为数组传递给call_user_func_array()时如何在一个数组中接收参数 - How to receive parameters in one array when passing arguments as an array to call_user_func_array()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM