Let’s say I have these two variables
$number = 1;
$word = "one";
and I want to use them in a pg_query.
This is what I’ve got:
$result = pg_query($con, 'UPDATE a SET z = ARRAY[{$number}] WHERE word = {pg_escape_literal($word)}');
But it doesn’t work..
To use string interpolation, you have to use double quotes:
You also can’t interpolate function calls into strings like you’re attempting with
{pg_escape_literal($word)}. You’ll need to escape the variable before interpolating it into the string:You could also use
sprintf:But the best and safest is to use
pg_query_paramsfunction, as you don’t escape any parameter. And it is very easy to forget and expose your site to SQL-injection attacks.