2013-10-31 49 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