PHP bindValue と bindParam


2020/5/7

PDOStatement::bindParam — 指定された変数名にパラメータをバインドする
PDOStatement::bindValue — 値をパラメータにバインドする

$stm = $pdo->prepare("select * from users where user = :user");
$user = "jack";
//
$stm->bindParam(":user",$user);
//エラー
//$stm->bindParam(":user","jack");

$stm->bindValue(":user",$user);

$stm->bindValue(":user","jack");

2020/5/8

bindValue と 「?」

<?php
/* バインドされた PHP 変数によってプリペアドステートメントを実行する */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < ? AND colour = ?');
$sth->bindValue(1, $calories, PDO::PARAM_INT);
$sth->bindValue(2, $colour, PDO::PARAM_STR);
$sth->execute();
?>