PDO 通过引用通知?

PDO pass by reference notice?(PDO 通过引用通知?)
本文介绍了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 通过引用通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Why can#39;t I update data in an array with foreach loop?(为什么我不能用 foreach 循环更新数组中的数据?)
Foreach for arrays inside of an array(Foreach 用于数组内的数组)
PHP array get next key/value in foreach()(PHP 数组在 foreach() 中获取下一个键/值)
Using preg_match on a multidimensional array to return key values arrays(在多维数组上使用 preg_match 返回键值数组)
php foreach as key, every two number as a group(php foreach 为key,每两个数字为一组)
Treat a PHP class that implements Iterator as an array(将实现 Iterator 的 PHP 类视为数组)