資料載入中

胡言亂語

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

以上紀錄。
 

https://innstory.com/story-PDObindParam和bindValue差別在哪裡-2375

上一篇
 codeing像極了愛情

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

發表留言

作者簡介

離不開電腦的宅男


推薦閱讀

作者其他相關類別故事

Linux下查看版本號指令

Linux下查看版本號指令

Mark Chang 7 年又 106 天 1.7K

其他指令查詢方式 以上紀錄~

將Google字型fonts.googleapis.com新增到CSP header 中

將Google字型fonts…

Mark Chang 4 年又 146 天 1.5K

Content Security Policy (CSP) 內容安全政策 主要用來限制網頁中對外部...

PHP重新排列陣列中的元素shuffle函數

PHP重新排列陣列中的元素s…

Mark Chang 3 年又 274 天 1.2K

shuffle() 是可以將陣列中元素的順序重新洗牌的函數。 甚麼時候用的到? 例如最近寫著一個...