本文介紹了帶有準(zhǔn)備好的語句的 PDO bindParam() 不起作用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
好的,這就是問題所在:
這有效:
$STH = $DBH->prepare("SELECT * FROM juegos WHERE id = 1");$STH->execute();
這不會:
$STH = $DBH->prepare("SELECT * FROM juegos WHERE id = :id");$STH->bindParam(':id', '1', PDO::PARAM_STR);$STH->execute();
我到底做錯了什么?它甚至不拋出異常
謝謝大家!
另外,這是完整的代碼
setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );$STH = $DBH->prepare("SELECT * FROM juegos WHERE id = :id");$STH->bindParam(':id', '1', PDO::PARAM_STR);$STH->execute();$STH->setFetchMode(PDO::FETCH_ASSOC);while($row = $STH->fetch()) {echo $row['nombre']."<br/>";}$DBH = 空;echo "Todo salió bien";} catch (PDOException $e) {echo "錯誤";}?>
解決方案
使用 bindParam()
變量是 綁定為引用.
字符串不能通過引用傳遞.>
可以通過引用傳遞以下內(nèi)容:
<塊引用>變量,即 foo($a)
新語句,即 foo(new foobar())
從函數(shù)返回的引用
嘗試使用 bindValue()
>
$STH->bindValue(':id', '1', PDO::PARAM_STR);
Ok, this is the problem:
This works:
$STH = $DBH->prepare("SELECT * FROM juegos WHERE id = 1");
$STH->execute();
This doesn't:
$STH = $DBH->prepare("SELECT * FROM juegos WHERE id = :id");
$STH->bindParam(':id', '1', PDO::PARAM_STR);
$STH->execute();
What in the world am I doing wrong? It doesn't even throw an exception
Thank you everyone!
Also, this is the whole code
<?php
try {
$DBH = new PDO("everything is", "ok", "here");
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$STH = $DBH->prepare("SELECT * FROM juegos WHERE id = :id");
$STH->bindParam(':id', '1', PDO::PARAM_STR);
$STH->execute();
$STH->setFetchMode(PDO::FETCH_ASSOC);
while($row = $STH->fetch()) {
echo $row['nombre']."<br/>";
}
$DBH = null;
echo "Todo salió bien";
} catch (PDOException $e) {
echo "Error";
}
?>
解決方案
Using bindParam()
the variable is bound as a reference.
A string can't be passed by reference.
The following things can be passed by reference:
Variables, i.e. foo($a)
New statements, i.e. foo(new foobar())
References returned from functions
Try using bindValue()
$STH->bindValue(':id', '1', PDO::PARAM_STR);
這篇關(guān)于帶有準(zhǔn)備好的語句的 PDO bindParam() 不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!