問題描述
這個(gè):
$stmt = $dbh->prepare("SELECT thing FROM table WHERE color = :color");
$stmt->bindParam(':color', $someClass->getColor());
$stmt->execute();
產(chǎn)生這個(gè):
運(yùn)行時(shí)通知
只應(yīng)傳遞變量參考
Runtime notice
Only variables should be passed by reference
雖然它仍然執(zhí)行.
這個(gè):
$stmt = $dbh->prepare("SELECT thing FROM table WHERE color = :color");
$tempColor = $someClass->getColor();
$stmt->bindParam(':color',$tempColor);
$stmt->execute();
毫無(wú)怨言地運(yùn)行.
我不明白其中的區(qū)別?
推薦答案
bindParam 的第二個(gè)參數(shù)是一個(gè)變量 參考.由于函數(shù)返回不能被引用,它不能嚴(yán)格滿足bindParam參數(shù)的需求(PHP會(huì)配合你,這里只會(huì)發(fā)出警告).
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).
為了更好地理解,這里有一個(gè)例子:這段代碼將產(chǎn)生與你的第二個(gè)例子相同的結(jié)果:
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();
這在函數(shù)返回的情況下是不可能的.
That won't be possible with a function return.
這篇關(guān)于PDO 通過引用通知?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!