7

我有以下代码例如:

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

此代码从execute()我设置PDO::ATTR_EMULATE_PREPARESFALSE. 异常对象中包含以下消息:

SQLSTATE[HY093]:参数号无效

虽然阅读了相应的手册,但目前无法意识到问题所在。

4

1 回答 1

9

该错误是由于重复占位符造成的。每个占位符必须是唯一的,即使您将相同的参数绑定到它。

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);
于 2013-01-12T14:44:02.533 回答