Loading...

PDO bindParam 和 bindValue 差別在哪裡?

Temperature: 0 °C

Mark ChangMark Chang
author_tools

首先 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-PDO_bindParam_和_bindValue_差別在哪裡-2375
PHP

Prev
 codeing_像極了愛情

Next
要如何知道ajax執行失敗原因 

About the Author

Mark Chang

離不開電腦的宅男

Visitor message

Leave some footprints to prove that you visited me

Recommended reading

Author's other related stories

Android 禁止螢幕旋轉

Android 禁止螢幕旋轉...

首先找到AndroidManifest.xml檔案 位置在app/manifests/路徑底下 在 ...

分享 斜槓青年正夯,什麼都會才是王道?他舉賈伯斯、巴菲特為例,勸你萬萬不可跨領域-風傳媒

分享 斜槓青年正夯,什麼都會...

巴菲特:「了解自己的能力圈,待在裡面。這個圈圈到底有多大其實無關緊要。知道圈圈的邊線究竟在哪則非常重...

Font Awesome Icon Sizes

Font Awesome I...

pexelsphotoicon5 紀錄一下Font Awesome icon尺寸設定 faxs fa...

Please select an option

error

Hi, thank you for your participation, but you cannot vote repeatedly~

Join innstory now and start recording your story.

"Innstory" is a place to store stories. We are committed to becoming a warm platform. Deepening the bonds between people is our direction.
We are convinced that the blockchain between people is not just a cold calculation. Join us now.

Wrong format