bindParamとbindValueの違いは何ですか?質問する

bindParamとbindValueの違いは何ですか?質問する

ベストアンサー1

から手動入力PDOStatement::bindParam:

[ の場合bindParam] とは異なりPDOStatement::bindValue()、変数は参照としてバインドされ、 が呼び出されたときにのみ評価されますPDOStatement::execute()

たとえば、次のようになります。

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

または

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

おすすめ記事