简体   繁体   English

PDO 错误:SQLSTATE[HY000]:一般错误:2031

[英]PDO error: SQLSTATE[HY000]: General error: 2031

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 ).查询包含占位符 ( :placeholder )。 But to add those LIMIT placeholders, I need to use the manual method ( bindValue ) because otherwise the engine will turn them into strings.但是要添加那些 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()

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');

..so, beware. ..所以,当心。

You cannot use ->bind* and ->execute($params) .您不能使用->bind*->execute($params) Use either or;使用或; if you pass parameters to execute() , those will make PDO forget the parameters already bound via ->bind* .如果您将参数传递给execute() ,这些将使 PDO 忘记已经通过->bind*绑定的参数。

This exception also appears if you try to run a query with placeholders instead of preparing a statment such as如果您尝试使用占位符运行查询而不是准备诸如

$stmt = $db->query('SELECT * FROM tbl WHERE ID > ?');

instead of代替

$stmt = $db->prepare('SELECT * FROM tbl WHERE ID > ?');

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调用 PDOStatement::bindParam() 将 PHP 变量绑定到参数标记:绑定变量将它们的值作为输入传递并接收其关联参数标记的输出值(如果有)

  • or pass an array of input-only parameter values或传递一组仅限输入的参数值

You need to pick a method.你需要选择一种方法。 You cannot mix both.你不能混合两者。

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);

So better use所以更好地使用

$sth->bindValue(':page_1', $page1);

This happens if you have mismatching parameters.如果您的参数不匹配,就会发生这种情况。 For example:例如:

$q = $db->prepare("select :a, :b");
$q->execute([":a"=>"a"]);

当您的 SQL 尝试更新 AUTO_INCREMENT 字段时,也会发生异常(至少在 MySQL/PDO 中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM