0

I'm getting this problem when connecting to a database. I'm trying to write a query containing sub-queries to run on Postgres, usind PDO. Unfortunately I get an "Invalid parameter number: :sensor"

    $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'));

I think my problem is due to the escaping of the ' characters surrounding the :(item). I've tried using \ but that then creates a syntax error. I presume there's a convention I'm unaware of for PHP to successfully substitute the string and then it not cause an error in Postgres.

Thanks for looking at this, James

1
  • 1
    use in instead of = also this many sub queries will kill your db Commented Oct 31, 2013 at 13:41

1 Answer 1

2

Try removing the single quotes surrounding the name of the parameters, something like this:

From => ':sensor' To => :sensor

In this link you will find an explanation.

Sign up to request clarification or add additional context in comments.

3 Comments

If you mean in the query, it doesn't work, I get this error,Exception called. SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "%" LINE 19: where sp_description ilike %$3%
Try to remove the % sighs from the SQL statements and put them in the arguments, i.e. ':name' => '%school1%'));
Ah, the combination of moving the % signs into the variable and unquoting in the query worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.