一尘不染

bindParam和bindValue有什么区别?

php


阅读 249

收藏
2020-05-26

共1个答案

一尘不染

答案在文档中bindParam

与PDOStatement :: bindValue()不同,该变量被绑定为引用,并且仅在调用PDOStatement ::
execute()时进行评估。

execute

调用PDOStatement :: bindParam()将PHP变量绑定到参数标记:绑定变量将其值作为输入传递,并接收其关联参数标记的输出值(如果有)

例:

$value = 'foo';
$s = $dbh->prepare('SELECT name FROM bar WHERE baz = :baz');
$s->bindParam(':baz', $value); // use bindParam to bind the variable
$value = 'foobarbaz';
$s->execute(); // executed with WHERE baz = 'foobarbaz'

要么

$value = 'foo';
$s = $dbh->prepare('SELECT name FROM bar WHERE baz = :baz');
$s->bindValue(':baz', $value); // use bindValue to bind the variable's value
$value = 'foobarbaz';
$s->execute(); // executed with WHERE baz = 'foo'
2020-05-26