資料載入中

胡言亂語

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執行失敗原因 

發表留言

作者簡介

離不開電腦的宅男


推薦閱讀

作者其他相關類別故事

解決iphone 不能使用colorbox 滑動的方式

解決iphone 不能使用c…

Mark Chang 7 年又 357 天 2.5K

以上紀錄

RPO(Relative Path Overwrite)相對路徑覆蓋攻擊

RPO(Relative P…

Mark Chang 6 年又 89 天 2.6K

首先,先來用例子說明何謂「相對路徑」何謂「絕對路徑」。 例如你有一個網址為http://www.de...

PHP使用array_product函數求得陣列中的乘積

PHP使用array_pro…

Mark Chang 3 年又 248 天 1.3K

arrayproduct()是PHP中的一個內建函數,可以返回陣列中所有元素的乘積。 例如 若想要...