【发布时间】:2020-04-23 10:51:46
【问题描述】:
我想使用call_user_func_array() 执行myqsli_stmt_bind_param,因为我有应该传递给查询的动态值。
首先,我尝试直接将我的$bind_params[] = [$type, $value](不带&)作为call_user_func_array() 的第二个参数。
第二次尝试我将$bind_params[] = [&$type, &$value] 作为call_user_func_array() 的第二个参数。
我的第一次试用是这样的(不带 &):
// To store all collected type into $bind_params[].
$bind_params = array($bind_params_1);
$count = count($bind_params_2);
// To store the all values into $bind_params[].
// $bind_params_2[] is collected $_POST values.
for ($i=0; $i < $count; $i++) {
$bind_params[] = $bind_params_2[$i];
$i++;
}
// Doing $stmt->bind_params($bind_params_1, $bind_params_2).
call_user_func_array(array($stmt, "bind_param"), $bind_params);
$stmt->execute();
如果我运行上面的代码,这将产生一个警告和一个错误:
警告:mysqli_stmt::bind_param() 的参数 2 应为 参考,给出的值 C:\xampp\htdocs\dashboard\x\xx\xxx\xxxx\xxxxx\gb_daftar_usulan.php 第 571 行
致命错误:在布尔值中调用成员函数 fetch_assoc() C:\xampp\htdocs\dashboard\x\xx\xxx\xxxx\xxxxx\gb_daftar_usulan.php 第 584 行
然后我的第二次试用看起来像这样(带 &):
// To store all collected type into $bind_params[].
$bind_params = array(&$bind_params_1);
// $bind_params_2[] is collected $_POST values.
$count = count($bind_params_2);
// To store the all values into $bind_params[].
for ($i=0; $i < $count; $i++) {
$bind_params[] = &$bind_params_2[$i];
$i++;
}
// Doing $stmt->bind_params($bind_params_1, $bind_params_2).
call_user_func_array(array($stmt, "bind_param"), $bind_params);
$stmt->execute();
第二个代码为我产生了正确的结果。 我从这里找到了这种方法:(https://stackoverflow.com/a/24713481)。
为什么会这样?
&$bind_params_1 和 &$bind_params_2 中的 & 到底是什么意思?
为什么我的第一次试用会出现警告和错误?
【问题讨论】:
-
阅读references的PHP文档
-
如果您仍在使用 PHP 5,我强烈建议您尽快升级。不再支持此版本。 Let Rasmus Lerdorf explain it to you
-
您在链接的主题中看到其他答案了吗?这个:stackoverflow.com/a/50682298/1839439
-
@Barmar 我知道 &$var 是为了在函数调用 $var 变量时对其进行更改。但我不明白为什么会这样。因为我在这两个试验中都“回显”了 $bind_params[],所以它们返回了相同的数组元素。
-
@Dharman 感谢您的建议兄弟。但是很遗憾我公司还是想用这个php版本,:(
标签: php mysqli prepared-statement php-5.4