好的,这是问题所在:
这有效:
$STH = $DBH->prepare("SELECT * FROM juegos WHERE id = 1");
$STH->execute();
Run Code Online (Sandbox Code Playgroud)
这不是:
$STH = $DBH->prepare("SELECT * FROM juegos WHERE id = :id");
$STH->bindParam(':id', '1', PDO::PARAM_STR);
$STH->execute();
Run Code Online (Sandbox Code Playgroud)
世界上我做错了什么?它甚至没有抛出异常
谢谢大家!
此外,这是整个代码
<?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";
}
?>
Run Code Online (Sandbox Code Playgroud)
dav*_*han 20
使用bindParam()变量作为参考.
字符串不能通过引用传递.
以下内容可以通过引用传递:
变量,即foo($ a)
新陈述,即foo(new foobar())
从函数返回的引用
尝试使用 bindValue()
$STH->bindValue(':id', '1', PDO::PARAM_STR);
Run Code Online (Sandbox Code Playgroud)
小智 5
PHPbindParam()将 PHP 变量绑定到用于准备语句的 SQL 语句中相应的命名或问号占位符。
正确的使用方法bindParam是:
$id = 1;
$sth = $DBH->prepare("SELECT * FROM juegos WHERE id = :id");
$sth->bindParam(':id', $id, PDO::PARAM_INT);// use bindParam to bind the variable
// ^ PDO::PARAM_INT - the value of the variable $id should be an int
// ^ $id - the variable being represented by ':id',
// ^ :id - represents the variable
// $id - the variable being represented by ':id',
Run Code Online (Sandbox Code Playgroud)
PHPbindValue()将值绑定到用于准备语句的 SQL 语句中相应的命名或问号占位符。
$id=10;
$name=roadkill;
$sth = $dbh->prepare('SELECT *
FROM juegos
WHERE id < :id AND name = :name');
$sth->bindValue(':id', $id, PDO::PARAM_INT);// use bindValue to bind the variable's value
$sth->bindValue(':name', $name, PDO::PARAM_STR);// use bindValue to bind the variable's value
Run Code Online (Sandbox Code Playgroud)
这两种方法之间的主要区别是,不像PDOStatement::bindValue()与bindParam()可变绑定作为参考,并且将仅在该时间进行评价PDOStatement::execute()时被调用。
| 归档时间: |
|
| 查看次数: |
40686 次 |
| 最近记录: |