php PDO 错误:SQLSTATE[HY000]:一般错误:2031
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17274556/
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 error: SQLSTATE[HY000]: General error: 2031
提问by silkfire
I'm getting this annoying error and although I have an idea of why I'm getting it, I can't for the life of me find a solution to it.
我遇到了这个烦人的错误,虽然我知道为什么会遇到它,但我终生无法找到解决方案。
if ($limit) {
$sth->bindValue(':page', $page - 1, PDO::PARAM_INT);
$sth->bindValue(':entries_per_page', $page * $entries_per_page, PDO::PARAM_INT);
}
$sth->execute($criteria);
Query contains placeholders (:placeholder
). But to add those LIMIT placeholders, I need to use the manual method (bindValue
) because otherwise the engine will turn them into strings.
查询包含占位符 ( :placeholder
)。但是要添加那些 LIMIT 占位符,我需要使用手动方法 ( bindValue
),否则引擎会将它们转换为字符串。
I'm not getting the Invalid number of parameters error, so all placeholders have been bound correctly (I assume).
我没有收到 Invalid number of parameters 错误,因此所有占位符都已正确绑定(我假设)。
Query:
询问:
SELECT `articles`.*, `regional_municipalities`.`name` AS `regional_municipality_name`,
`_atc_codes`.`code` AS `atc_code`, `_atc_codes`.`name` AS `substance`
FROM `articles`
LEFT JOIN `_atc_codes`
ON (`_atc_codes`.`id` = `articles`.`atc_code`)
JOIN `regional_municipalities`
ON (`regional_municipalities`.`id` = `articles`.`regional_municipality`)
WHERE TRUE AND `articles`.`strength` = :strength
GROUP BY `articles`.`id`
ORDER BY `articles`.`id`
LIMIT :page, :entries_per_page
All placeholder values reside in $criteria, except for the last two LIMIT, which I manually bind with bindValue()
.
所有占位符值都位于 $criteria 中,除了最后两个 LIMIT,我手动绑定了bindValue()
.
回答by Nowdeen
This same error 2031 can be issued when one bind two values with the same parameter name, like in:
当使用相同的参数名称绑定两个值时,可能会发出相同的错误 2031,例如:
$sth->bindValue(':colour', 'blue');
$sth->bindValue(':colour', 'red');
$sth->bindValue(':colour', 'blue');
$sth->bindValue(':colour', 'red');
..so, beware.
..所以,当心。
回答by deceze
You cannot use ->bind*
and->execute($params)
. Use either or; if you pass parameters to execute()
, those will make PDO forget the parameters already bound via ->bind*
.
您不能使用->bind*
和->execute($params)
。使用或;如果您将参数传递给execute()
,这些将使 PDO 忘记已经通过->bind*
.
回答by AbcAeffchen
This exception also appears if you try to run a querywith placeholders instead of preparinga statment such as
如果您尝试使用占位符运行查询而不是准备诸如
$stmt = $db->query('SELECT * FROM tbl WHERE ID > ?');
instead of
代替
$stmt = $db->prepare('SELECT * FROM tbl WHERE ID > ?');
回答by álvaro González
From the manual:
从手册:
public bool PDOStatement::execute ([ array $input_parameters ] )
Execute the prepared statement. If the prepared statement included parameter markers, you must either:
call PDOStatement::bindParam() to bind PHP variables to the parameter markers: bound variables pass their value as input and receive the output value, if any, of their associated parameter markers
or pass an array of input-only parameter values
public bool PDOStatement::execute ([ array $input_parameters ] )
执行准备好的语句。如果准备好的语句包含参数标记,您必须要么:
调用 PDOStatement::bindParam() 将 PHP 变量绑定到参数标记:绑定变量将它们的值作为输入传递并接收其关联参数标记的输出值(如果有)
或传递一组仅限输入的参数值
You need to pick a method. You cannot mix both.
你需要选择一种方法。你不能混合两者。
回答by Martin Schilliger
It's not exactly an answer, but this error also happens if you try to use a word with a hyphen as placeholders, for example:
这不完全是一个答案,但是如果您尝试使用带有连字符的单词作为占位符,也会发生此错误,例如:
$sth->bindValue(':page-1', $page1);
$sth->bindValue(':page-1', $page1);
So better use
所以更好地使用
$sth->bindValue(':page_1', $page1);
$sth->bindValue(':page_1', $page1);
回答by Charlie
This happens if you have mismatching parameters. For example:
如果您的参数不匹配,就会发生这种情况。例如:
$q = $db->prepare("select :a, :b");
$q->execute([":a"=>"a"]);
回答by Roadowl
The exception also happens (at least in MySQL/PDO) when your SQL tries to UPDATE an AUTO_INCREMENT field.
当您的 SQL 尝试更新 AUTO_INCREMENT 字段时,也会发生异常(至少在 MySQL/PDO 中)。