pq_query Syntax Error because of a hyphen. How do i write this?

969 Views Asked by At

This does not work

$query = "SELECT * FROM time-lords WHERE user='thedoctor'";

I get this error:

Warning: pg_query(): Query failed: ERROR: syntax error at or near "-"

This one does work

$query = "SELECT * FROM time_lords WHERE user='thedoctor'";

So I suppose the issue is the hyphen. What is the correct way to write this? I've tried wrapping the column name in just about everything....."time-lords", [time-lords], `time-lords` and nothing seems to work.

2

There are 2 best solutions below

0
On BEST ANSWER

Thanks! That solved the syntax error. To get the query to filter I had to do the escape double quote on the column as well:

$query = "SELECT * FROM \"time-lords\" WHERE \"user\"='thedoctor'";
0
On

You can escape object names with double quotes ("):

$query = "SELECT * FROM \"time-lords\" WHERE user='thedoctor'";