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.
相关文章