Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

' or " in PHP's Postgres queries

Tags:

php

postgresql

  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.

like image 400
Léo Léopold Hertz 준영 Avatar asked Apr 27 '26 04:04

Léo Léopold Hertz 준영


2 Answers

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?
like image 130
Brian Ramsay Avatar answered Apr 29 '26 17:04

Brian Ramsay


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\''
like image 33
Luc M Avatar answered Apr 29 '26 18:04

Luc M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!