PDO bindParam() with prepared statement isn#39;t working(带有准备好的语句的 PDO bindParam() 不起作用)
问题描述
好的,这就是问题所在:
这有效:
$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()
变量是 绑定为引用.
字符串不能通过引用传递.>
可以通过引用传递以下内容:
<块引用>变量,即 foo($a)
新语句,即 foo(new foobar())
从函数返回的引用
尝试使用 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);
这篇关于带有准备好的语句的 PDO bindParam() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有准备好的语句的 PDO bindParam() 不起作用
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01