Search code examples
phpsqlpostgresqlpg-query

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


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.


Solution

  • 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'";