0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

bindParamとbindValueの違い

0
Last updated at Posted at 2025-11-12

はじめに

PHP基礎入門でPDOを学習中、bindParamとbindValueの違いが気になったので整理しました。
同じように悩んでいる方の参考になれば幸いです。

結論

  • 値が確定している場合bindValueを使う
  • 同じステートメントで変数の値を変えて複数回実行する場合

書籍の内容に関してはフォームで入力されてきた値で固定されるためbindValueでも問題なさそう。

もう少し詳しく

bindValue:値をバインド

$stmt = $pdo->prepare("INSERT INTO users (name, age) VALUES (:name, :age)");

$name = "太郎";
$age = 25;

$stmt->bindParam(':name', $name);
$stmt->bindParam(':age', $age);

$name = "花子"; // ここで変更すると影響する!
$stmt->execute(); // "花子", 25 が登録される

bindParam:変数をバインド(参照渡し)

$stmt = $pdo->prepare("INSERT INTO users (name, age) VALUES (:name, :age)");

$name = "太郎";
$age = 25;

$stmt->bindParam(':name', $name);
$stmt->bindParam(':age', $age);

$name = "花子"; // ここで変更すると影響する!
$stmt->execute(); // "花子", 25 が登録される
bindParam() bindValue()
値をバインドするタイミング SQL実行時 (execute() 時) に評価される バインドした瞬間に値が確定す
渡すもの 変数(参照渡し) 値(もしくは変数の中身)
使う場面 ループで値を変えながら実行する時 値が確定している場合(普通はこちらが多い)

参考

  • PHP基礎入門(MdN)
  • PHP公式ドキュメント

#PHP #初心者 #PDO

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?