What is the difference between bindParam and bindValue?

后端 未结 7 1264
一向
一向 2020-11-22 00:23

What is the difference between PDOStatement::bindParam() and PDOStatement::bindValue()?

7条回答
  •  渐次进展
    2020-11-22 01:11

    From the manual entry for PDOStatement::bindParam:

    [With bindParam] Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.

    So, for example:

    $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'
    

    or

    $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'
    

提交回复
热议问题