将PDO :: ATTR_EMULATE_PREPARES更改为FALSE并获取“无效参数编号”错误

时间:2013-01-12 13:38:43

标签: php mysql pdo prepared-statement

我有以下代码:

$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_PREPARES时,此代码会引发FALSE的异常。异常对象中包含以下消息:

  

SQLSTATE [HY093]:参数号无效

到目前为止无法实现这个问题,尽管阅读了相应的手册。

1 个答案:

答案 0 :(得分: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);