基本的な使い方


prepare("select * from TABLE where id = :ID");
//$sth = $pdo->prepare("select * from TABLE where id = ?");

$id = 100;

//$sth->bindParam(":ID", $id, PDO::PARAM_INT); // bindParam()は参照渡し
//$sth->bindParam(1, $id, PDO::PARAM_INT);
$sth->bindValue(":ID", 102, PDO::PARAM_INT); // bindValue()ではこう(直書きで)渡せる

$sth->execute();
//$sth->execute(array(100));

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
var_dump($row);
}
} catch (PDOException $e) {
$e->getMessage();
}

?>


それから


If you're using bindParam in a loop such as this:

$counter=1;
foreach($email as $val){
$stmt->bindParam($counter, $val, PDO::PARAM_STR);
$counter++;
}

It will fail because $val is local and the variable is bound as a reference and will only be evaluated at the time that PDOStatement->execute() is called.

So use bindValue instead.


参照渡しなので、しかもexecute()時に評価されるので、


$valの最後の値が全てのプレースホルダバインドされるんですね。