資料載入中

胡言亂語

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

發表留言

作者簡介

離不開電腦的宅男


推薦閱讀

作者其他相關類別故事

PHP 字串字數計算並隱藏

PHP 字串字數計算並隱藏

Mark Chang 7 年又 115 天 2.2K

使用PHP substr函式可以做字串切割,但在遇到中文字時記得使用mbsubstr來做編碼,才...

使用jquery判斷上傳檔案大小

使用jquery判斷上傳檔案…

Mark Chang 7 年又 25 天 1.7K

首先得引用jquery 上傳檔案若超過5MB,直接中斷。 以上紀錄。  

使用 Closure Compiler 幫 javascript 瘦身

使用 Closure Com…

Mark Chang 4 年又 115 天 1.7K

JavaScript(縮寫為JS)是一種進階的、直譯的程式語言,且作為客戶端手稿語言在使用者的瀏覽器...