0

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

4
  • 1
    Are you getting any errors? Show us some more code to support it does not work Commented Sep 14, 2016 at 6:10
  • Yes, sorry I added the error message. Commented Sep 14, 2016 at 6:13
  • Query need be like this:-$sql = "CREATE TABLE ".$rNumber."_Bus_Stops ( id int NOT NULL, name char(50) NOT NULL,x double,y double,CONSTRAINT ".$rNumber."_Bus_Stops PRIMARY KEY (id) );"; Commented Sep 14, 2016 at 6:16
  • @M_T Table names can't start with a number. Read this Commented Sep 14, 2016 at 6:17

1 Answer 1

1

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" (
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.