PDO中的bindParam和bindValue有什么区别?

bindParam

$sex = 'male';
$s = $dbh->prepare('SELECT name FROM students WHERE sex = :sex');
$s->bindParam(':sex', $sex); // use bindParam to bind the variable
$sex = 'female';
$s->execute(); // executed with WHERE sex = 'female'

bindValue

$sex = 'male';
$s = $dbh->prepare('SELECT name FROM students WHERE sex = :sex');
$s->bindParam(':sex', $sex); // use bindParam to bind the variable
$sex = 'female';
$s->execute(); // executed with WHERE sex = 'female'

PDOStatement::bindValue —
把一个值绑定到一个参数

绑定一个值到用作预处理的 SQL 语句中的对应命名占位符或问号占位符。

PDOStatement::bindParam —
绑定一个参数到指定的变量名

绑定一个PHP变量到用作预处理的SQL语句中的对应命名占位符或问号占位符。 不同于 PDOStatement::bindValue() ,此变量作为引用被绑定,并只在 PDOStatement::execute() 被调用的时候才取其值。

发布了7 篇原创文章 · 获赞 3 · 访问量 211

猜你喜欢

转载自blog.csdn.net/k851819815/article/details/104430911