2013-01-12 26 views
6

我有以下代碼forexample:更改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_PREPARESFALSE拋出異常。下面的消息被包括在異常對象:

SQLSTATE [HY093]:無效參數號

無法意識到問題,到目前爲止,雖然讀出的相應的手冊。

回答

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); 
+1

撞頭到磚牆/解決...謝謝。 –

+0

感嘆...有時候php是無法相信的......無論如何謝謝你的回答。你是對的! – StefanNch