PDO pass by reference notice?(PDO 通过引用通知?)
问题描述
这个:
$stmt = $dbh->prepare("SELECT thing FROM table WHERE color = :color");
$stmt->bindParam(':color', $someClass->getColor());
$stmt->execute();
产生这个:
运行时通知
只应传递变量参考
Runtime notice
Only variables should be passed by reference
虽然它仍然执行.
这个:
$stmt = $dbh->prepare("SELECT thing FROM table WHERE color = :color");
$tempColor = $someClass->getColor();
$stmt->bindParam(':color',$tempColor);
$stmt->execute();
毫无怨言地运行.
我不明白其中的区别?
推荐答案
bindParam 的第二个参数是一个变量 参考.由于函数返回不能被引用,它不能严格满足bindParam参数的需求(PHP会配合你,这里只会发出警告).
The second parameter of bindParam is a variable reference. Since a function return cannot be referenced, it fails to strictly meet the needs of the bindParam parameter (PHP will work with you though and will only issue a warning here).
为了更好地理解,这里有一个例子:这段代码将产生与你的第二个例子相同的结果:
To get a better idea, here's and example: this code will produce the same results as your second example:
$stmt = $dbh->prepare("SELECT thing FROM table WHERE color = :color");
$tempColor = NULL; // assigned here
$stmt->bindParam(':color',$tempColor);
$tempColor = $someClass->getColor(); // but reassigned here
$stmt->execute();
这在函数返回的情况下是不可能的.
That won't be possible with a function return.
这篇关于PDO 通过引用通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PDO 通过引用通知?


基础教程推荐
- 将变量从树枝传递给 js 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- php中的PDF导出 2022-01-01
- php中的foreach复选框POST 2021-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- Web 服务器如何处理请求? 2021-01-01