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

Dyi*_*yin 6 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();
Run Code Online (Sandbox Code Playgroud)

此代码抛出的异常execute(),当我设置PDO::ATTR_EMULATE_PREPARES到FALSE.异常对象中包含以下消息:

SQLSTATE [HY093]:参数号无效

到目前为止无法实现这个问题,虽然阅读了相应的手册.

Jon*_*ler 9

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

AND ((type='employer' AND owner_id=:02)
OR (type='employee' AND winner_id=:02))
Run Code Online (Sandbox Code Playgroud)

应该:

AND ((type='employer' AND owner_id=:02)
OR (type='employee' AND winner_id=:another02))
Run Code Online (Sandbox Code Playgroud)

然后绑定到它:

$dbStatement->bindParam(':01',$Type);
$dbStatement->bindParam(':02',$UserID);
$dbStatement->bindParam(':another02',$UserID);
$dbStatement->bindParam(':03',$Most);
Run Code Online (Sandbox Code Playgroud)