資料載入中

胡言亂語

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

發表留言

作者簡介

離不開電腦的宅男


推薦閱讀

作者其他相關類別故事

修改php設定

修改php設定

Mark Chang 7 年又 356 天 2K

進入系統 用文字編輯軟體打開php.ini改變以下設定 disablefunctions = s...

PHP清空陣列函數

PHP清空陣列函數

Mark Chang 7 年又 350 天 2K

將整個陣列清空,但之後如果再加入元素,其 index 是往上累加的,而非從 0 開始。 使用 un...

PDO PHP - PDOException - Numeric value out of range 1264 Out of range value for column user_ip at row 1

PDO PHP - PDOE…

Mark Chang 5 年又 92 天 1.6K

最近遇到一個問題,一個Mysql的錯誤訊息。 我想要紀錄一個IP資訊,我會先用以下方式擷取用戶端使用...