tags:

views:

100

answers:

2
  1. When should you use the character ' in PHP pg queries?
  2. When should you use the character " in PHP pg queries?

This question is based on this answer.

+3  A: 

String literals in Postgres are defined using single quotes. Double quotes are used around identifiers. So the following query is valid.

SELECT "id", "name" FROM my_table WHERE "name" = 'Brian'

However, judging by the answer that you linked to, you're asking about single quote ' vs double quote " in PHP strings, rather than postgres queries.

The same as normal strings, a string in double quotes will interpolate variables, while a string in single quotes will have exactly what you put in.

$my_var = "noob";

echo "This is a test string, $my_var\nGot it?";
>> This is a test string, noob
>> Got it?

echo 'This is a test string, $my_var\nGot it?';
>> This is a test string, $my_var\nGot it?
Brian Ramsay
I am not sure whether your examples apply in the PHP pg queries, since they are in different environments that is not inside a query. Your answer also conflicts with Vitaly's answer.
Masi
+2  A: 

Into a PostgreSQL query, you must use '

When you build a query in PHP, you may use ' or "

"select * from table where id = 'me'"

or

'select * from table where id = \'me\''
Luc M
**Which one is correct?** #1 `id = $1` OR #2 `id = '$1'`
Masi
@Masi: #1 is correct -- do not put query parameters in quotes.
Bill Karwin
@Luc M: You can use " in PostgreSQL query, but it applies to delimited identifiers, instead of string literals.
Bill Karwin