資料載入中

胡言亂語

PDO bindParam 和 bindValue 差別在哪裡

首先 bindParam 是綁定變數、bindValue  是綁定值。
以下範例:
bindParam
<?php
try {
$dbh = new PDO("mysql:host=localhost;dbname=test", "root", "");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("SELECT * FROM tablename WHERE number = ?");
$stmt->bindParam(1, $test_value, PDO::PARAM_INT);
$test_value = 888;
$stmt->execute();
print_r($stmt->fetchAll());
} catch(PDOException $e) {
echo $e->getMessage();
}
?>

執行正常。

bindValue
<?php
try {
$dbh = new PDO("mysql:host=localhost;dbname=test", "root", "");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("SELECT * FROM tablename WHERE number = ?");
$stmt->bindValue(1, $test_value, PDO::PARAM_INT);
$test_value = 888;
$stmt->execute();
print_r($stmt->fetchAll());
} catch(PDOException $e) {
echo $e->getMessage();
}
?>

輸出:Notice: Undefined variable: test_value

以上紀錄。
 

  • bindParam 與 bindValue 的差異
  • 綁定變數與綁定值的使用
  • PHP 變數綁定範例
  • 如何處理未定義變數錯誤
  • 綁定參數在 PHP 中的應用
https://innstory.com/story-PDObindParam和bindValue差別在哪裡-2375

上一篇
 codeing像極了愛情

下一篇
要如何知道ajax執行失敗原因 

發表留言

  • Jack

    文章提到的 bindParam 和 bindValue 的區別還挺有意思的,這樣的細節在寫程式時常常會影響結果。這對於調試確實很重要。
    2026-08-31 下午 8 點回覆

作者簡介

離不開電腦的宅男


推薦閱讀

作者其他相關類別故事

php使用PDO獲取結果的方法

php使用PDO獲取結果的方…

Mark Chang 6 年又 125 天 1.3K

式例如下: 式例如下: fetchstyle控制結果的返回方式,類別如下: 以上紀錄。

使用CSS 讓ul li 橫向水平

使用CSS 讓ul li 橫…

Mark Chang 5 年又 209 天 2.5K

好吧,其實這個css常在用,但我每次都沒特別紀錄,然後每次又忘記然後又要Google。 其實主要就是...

PostgreSQL對應MySQL注意事項,時間與日期格式是最容易踩雷的地方之一

PostgreSQL對應My…

Mark Chang 2026-06-23 下午 4 點 225

從 MySQL 轉到 PostgreSQL(或雙軌並行)時,時間與日期格式是最容易踩雷的地方之一。雖...