【问题标题】:What do & symbol and call_user_func_array(array($stmt, "bind_param"), $value) do in php? [duplicate]& 符号和 call_user_func_array(array($stmt, "bind_param"), $value) 在 php 中做什么?
【发布时间】: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)。

为什么会这样?

&amp;$bind_params_1&amp;$bind_params_2 中的 &amp; 到底是什么意思?

为什么我的第一次试用会出现警告和错误?

【问题讨论】:

  • 阅读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


【解决方案1】:

& 对变量的引用, 这意味着它将内存中的地址提供给 function() 并且不会将值复制到内存的新部分。它的工作原理在这里: Passing by Reference -> PHP-Docu 正如@Brama 所说,阅读References 的工作原理

您可以在 PHP 文档中找到该函数的示例说明 call_user_func_array ()

它返回回调的返回值或错误时返回 FALSE。

Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in C:\xampp\htdocs\dashboard\x\xx\xxx\xxxx\xxxxx\gb_daftar_usulan.php on line 571

Fatal error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\dashboard\x\xx\xxx\xxxx\xxxxx\gb_daftar_usulan.php on line 584

警告意味着无法处理预期的参数,因此,致命错误,因为fetch_assoc() 需要正确的 DB-result

您可以通过以下方式防止这种情况:

if ($result = $mysqli->query($query)) {

/* fetch associative array */
while ($row = $result->fetch_assoc()) {
    printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}

/* free result set */
$result->free();
}

source: PHP-Manual

【讨论】:

  • @Janick Koder 我已经尝试过您建议的代码先生。但是警告仍然出现。 ` 警告:mysqli_stmt::bind_param() 的参数 2 应为参考,值在 C:\xampp\htdocs\dashboard\x\xx\xxx\xxxx\xxxxx\gb_daftar_usulan.php 第 552 行中给出 ` Mybe 我应该使用参考(&$bind_params)。我不应该是兄弟吗?
  • @Dharman :感谢您提供的参考链接。这是值得的。
猜你喜欢
  • 2016-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
  • 2010-10-08
相关资源
最近更新 更多