sql – PHP Postgres Driver 9.2.18 problem with simple select-ThrowExceptions

Exception or error:

I never used postgres with PHP.
Now I’m trying a simple select on a view in Postgres, but I get this error:

An error occurred: Syntax Error in SELECT * FROM 02_s3_ct13t_tariffe

It seems that the connection is fine (if I run SELECT table_name FROM information_schema.tables it works, and I can see in results the table name 02_s3_ct13t_tariffe).

Instead, if I run this code, I get the error mentioned above:

$conn_string = "host=xxx.xxx.xxx.xxx port=xxxx dbname=xxxx user=xxxx password=xxxxxxxx";
$db = pg_connect("$conn_string") or die('connection failed');

//$sql = "SELECT table_name FROM information_schema.tables";
$sql = "SELECT * FROM 02_s3_ct13t_tariffe";
$result = pg_query($db, $sql);

if (!$result) {
  $error = pg_last_error($db);
  echo "$error";
  exit;
}

Any ideas? how to check privileges or correct syntax to access a table name starting with numbers?

How to solve:

Enclose your table in quotes (table names shouldn’t start with digits):

SELECT * FROM "02_s3_ct13t_tariffe"

Better use pdo_pqsql instead.

Leave a Reply

Your email address will not be published. Required fields are marked *