php 带有准备好的语句的 PDO bindParam() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19902078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDO bindParam() with prepared statement isn't working
提问by arielnmz
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";
}
?>
回答by david strachan
Using bindParam()
the variable is bound as a reference.
使用bindParam()
变量绑定为引用。
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
变量,即 foo($a)
新语句,即 foo(new foobar())
从函数返回的引用
Try using bindValue()
尝试使用 bindValue()
$STH->bindValue(':id', '1', PDO::PARAM_STR);
回答by hackedd
The value for the :tabla
parameter will be automatically quoted and escaped by PDO. The query executed would become:
:tabla
参数的值将被 PDO 自动引用和转义。执行的查询将变为:
SELECT * FROM 'juegos'
which is not valid SQL.
这不是有效的 SQL。
回答by Cristo Rutazihana
PHP bindParam()
binds a PHP variable to a corresponding named or question mark placeholder in the SQL statement that was used to prepare the statement.
PHPbindParam()
将 PHP 变量绑定到用于准备语句的 SQL 语句中相应的命名或问号占位符。
The correct way to use bindParam
is:
正确的使用方法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',
PHP bindValue()
binds a value to a corresponding named or question mark placeholder in the SQL statement that was used to prepare the statement.
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
The key difference between these two methods is that unlike PDOStatement::bindValue()
, with bindParam()
the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute()
is called.
这两种方法之间的主要区别是,不像PDOStatement::bindValue()
与bindParam()
可变绑定作为参考,并且将仅在该时间进行评价PDOStatement::execute()
时被调用。
回答by Vivek Tiwari
For me replacing the double quote by single quote fixed the issue.
对我来说,用单引号替换双引号解决了这个问题。
Previous:
以前的:
$STH = $DBH->prepare("SELECT * FROM statstracker WHERE SrNo = :id");
Solution:
解决方案:
$STH = $DBH->prepare('SELECT * FROM statstracker WHERE SrNo = :id');
And it works, though not sure why.
它有效,但不知道为什么。
Hope it helps!
希望能帮助到你!
回答by SAR
do not pass the value directly to BindParam.
不要将值直接传递给 BindParam。
try {
// $DBH = new PDO("everything is", "ok", "here");
$DBH = new PDO("mysql:host=localhost;dbname=test", 'root', '');
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$STH = $DBH->prepare("SELECT * FROM statstracker WHERE SrNo = :id");
$id = 1; // here you should keep it as variable and pass it to param
$STH->bindParam(':id', $id, PDO::PARAM_STR);
$STH->execute();
$STH->setFetchMode(PDO::FETCH_ASSOC);
while($row = $STH->fetch()) {
echo $row['SrNo']."<br/>";
}
$DBH = null;
echo "Todo salió bien";
} catch (PDOException $e) {
echo "Error";
}