2013-10-31 52 views
0

連接到數據庫時出現此問題。我正在嘗試編寫一個包含子查詢的查詢,以在Postgres,usind PDO上運行。不幸的是,我得到一個「無效參數號:傳感器」PDO Postgres無效的錯誤

$statement="select di_timestamp, di_item_value 
from data_item 
where 
fk_fc_id=(select fc_id 
     from field_column 
     where 
     fc_description is like ':sensor' 
     and 
     fk_mds_id=(select mds_id 
        from monitored_data_set 
        where fk_pa_id=(select pa_id 
         from pilot_ambient 
         where 
         pa_ambient_name ilike ':room' 
         and 
         fk_sp_id=(
          select sp_id 
          from School_Pilot 
          where sp_description ilike '%:name%' 
          ) 
         ) 
        ) 
       )"; 
$query = $databaseConn->prepare($statement); 

$query->execute(array(':sensor'=>'Room Temperature',':room'=>'rm1',':name' => 'school1')); 

我想我的問題是,因圍繞的'字符轉義:(項目)。我嘗試過使用\,但那會產生一個語法錯誤。我認爲有一個約定我不知道PHP成功替換字符串,然後它不會導致Postgres中的錯誤。

感謝您看這個, 詹姆斯

+1

使用'in'而不是'='也是這麼多的子查詢會殺死你的分區 –

回答

2

嘗試刪除周圍的參數的名稱,像這樣的單引號:

從=>':sensor'爲了=>:sensor

在這link你會找到一個解釋。

+0

如果你的意思是在查詢中,它不起作用,我得到這個錯誤,Exception調用。 SQLSTATE [42601]:語法錯誤:7錯誤:語法錯誤處於或接近「%」 線19:其中sp_description ilike%$ 3% – James

+0

嘗試從SQL語句中刪除%sighs並將其放入參數中,即' ':name'=>'%school1%'));' –

+0

啊,將%符號移入變量並在查詢中取消引用的組合。 – James