' or " in PHP's Postgres queries

  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.

Upvotes: 3

Views: 728

Answers (2)

Brian Ramsay
Brian Ramsay

Reputation: 7635

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?

Upvotes: 4

Luc M
Luc M

Reputation: 17314

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\''

Upvotes: 3

Related Questions