Using LIKE wildcards inside pg_prepare

php, postgresql-8.4, string

Solution

I've had the same issue binding parameters using PDO adapters. The solution is to pass the "%" in with the variable:

$query = pg_prepare($conn, "MyStatement", 
'SELECT "Query" from "MyTable" 
 WHERE "Query" LIKE $1 
 ORDER BY "MyColumn" DESC;');

$result = pg_execute($conn, "MyStatement", array($my_param."%"));

If you need

...LIKE '%param%' ...

Then your query would be:

$result = pg_execute($conn, "MyStatement", array("%".$my_param."%"));

Problem

I have been trying to use LIKE inside a prepared statement, but php won't execute the statement because of a syntax error due to the use of the wildcard %. Here is the code ``` $query = pg_prepare($conn, "MyStatement", 'SELECT "Query" from "MyTable" WHERE "Query" LIKE $1% ORDER BY "MyColumn" DESC;'); $result = pg_execute($conn, "MyStatement", array($my_param)); ``` The thing is that php shows me a warning in the second line claiming a syntax error.

Original source