将PDO :: ATTR_EMULATE_PREPARES更改为FALSE并获取“无效参数号".错误 [英] Changed PDO::ATTR_EMULATE_PREPARES to FALSE and getting "Invalid parameter number" error

查看:56
本文介绍了将PDO :: ATTR_EMULATE_PREPARES更改为FALSE并获取“无效参数号".错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有以下代码:

$dbStatement=$this->dbObject->prepare("SELECT AVG(quality) as quality,
                                              AVG(adequacy) as adequacy,
                                              AVG(friendliness) as friendliness,
                                              SUM(overall) as overall,
                                              SUM(completed) as completed,
                                              type
                                       FROM   (SELECT AVG(quality) as quality,
                                                      AVG(adequacy) as adequacy,
                                                      AVG(friendliness) as friendliness,
                                                      COUNT(id) as overall,
                                                      SUM(is_completed) as completed,
                                                      category_id, type
                                               FROM valuation a
                                               WHERE status       =1
                                                 AND type         =:01
                                                 AND ((type='employer' AND owner_id=:02)
                                                      OR (type='employee' AND winner_id=:02))
                                               GROUP BY category_id
                                               HAVING COUNT(id)<=:03) b
                                       GROUP BY type");
$dbStatement->bindParam(':01',$Type);
$dbStatement->bindParam(':02',$UserID);
$dbStatement->bindParam(':03',$Most);
$dbStatement->execute();

当我将PDO::ATTR_EMULATE_PREPARES设置为FALSE时,此代码从execute()引发异常.异常对象中包含以下消息:

This code throws an exception from execute() when I set PDO::ATTR_EMULATE_PREPARES to FALSE. The following message is included in the exception object:

SQLSTATE [HY093]:无效的参数号

SQLSTATE[HY093]: Invalid parameter number

尽管已阅读相应的手册,但到目前为止仍无法解决问题.

Couldn't realize the problem so far, though read the corresponding manuals.

推荐答案

该错误是由于重复占位符引起的.每个占位符都必须是唯一的,即使您将相同的参数绑定到该占位符.

The error is due to repetition of a placeholder. Each placeholder must be unique, even if you are binding the same parameter to it.

AND ((type='employer' AND owner_id=:02)
OR (type='employee' AND winner_id=:02))

应该是:

AND ((type='employer' AND owner_id=:02)
OR (type='employee' AND winner_id=:another02))

然后绑定到它:

$dbStatement->bindParam(':01',$Type);
$dbStatement->bindParam(':02',$UserID);
$dbStatement->bindParam(':another02',$UserID);
$dbStatement->bindParam(':03',$Most);

这篇关于将PDO :: ATTR_EMULATE_PREPARES更改为FALSE并获取“无效参数号".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆