M_T
M_T

Reputation: 65

How to add a php variable to a postgres query?

I want to create a table by the name of the number that user creates. So what I wrote as the Postgres query is

$sql = 'CREATE TABLE '.$rNumber.'_Bus_Stops(
                id serial primary key,
                name character varying,
                x double precision,
                y double precision
                );';  

I'm getting this error.

Warning: pg_query(): Query failed: ERROR: syntax error at or near "11" LINE 1: CREATE TABLE 11_Bus_Stops( ^ in C:\wamp\www\cdap4\route1.php on line 138

Tried the same with double quotes as well. It does not work. Can someone help? It works totally fine if I remove $rNumber

Upvotes: 0

Views: 341

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246278

Since the table name starts with a digit, it has to be a quoted identifier (an identifier surrounded with double quotes):

$sql = 'CREATE TABLE "'.$rNumber.'_Bus_Stops" (

Upvotes: 1

Related Questions