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

PDO error: SQLSTATE[HY000]: General error: 2031(PDO 错误:SQLSTATE[HY000]:一般错误:2031)
本文介绍了PDO 错误:SQLSTATE[HY000]:一般错误: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);

查询包含占位符(:placeholder).但是要添加那些 LIMIT 占位符,我需要使用手动方法 (bindValue),否则引擎会将它们转换为字符串.

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.

我没有收到 Invalid number of parameters 错误,因此所有占位符都已正确绑定(我假设).

I'm not getting the Invalid number of parameters error, so all placeholders have been bound correctly (I assume).

查询:

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

所有占位符值都位于 $criteria 中,除了最后两个 LIMIT,我用 bindValue() 手动绑定.

All placeholder values reside in $criteria, except for the last two LIMIT, which I manually bind with bindValue().

推荐答案

你不能使用 ->bind* and ->execute($params).使用或;如果你将参数传递给 execute(),那些会让 PDO 忘记已经通过 ->bind* 绑定的参数.

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*.

这篇关于PDO 错误:SQLSTATE[HY000]:一般错误:2031的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)