PDO bindParam 与 bindValue 的区别

2023-10-22 15:40:56 浏览数 (1)

官方文档 https://www.php.net/manual/zh/pdostatement.bindparam.php https://www.php.net/manual/zh/pdostatement.bindvalue.php

先说结论

PDOStatement::bindParam 是引用,&

示例

代码语言:javascript复制
$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'
代码语言:javascript复制
$sex = 'male';
$s = $dbh->prepare('SELECT name FROM students WHERE sex = :sex');
$s->bindValue(':sex', $sex); // use bindValue to bind the variable's value
$sex = 'female';
$s->execute(); // executed with WHERE sex = 'male'

以上两个案例的区别,bindParam 中 $sex 取值,是最后赋值的 'female',而 bindValue 是执行语句时的 'male'

0 人点赞