Hello, I am trying to create a table in MySQL with the following data:

 $sql = "CREATE TABLE $uID 
(
Col0 int(10) NOT NULL auto_increment,
Col1 varchar(255) NOT NULL,
Col2 varchar(255) NOT NULL,
Col3 varchar(255) NOT NULL,
PRIMARY KEY(Col0)
)";

However, I keep getting a syntax error. Can someone pls. help me

[--EDIT--]
The error that is spitted out is:

Error creating table: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Col2 varchar(255) NOT NULL, Col3 varchar(255) NOT NULL, PRIMARY KEY(Col0) )' at line 5

Recommended Answers

All 8 Replies

The syntax is fine. I was able to create the table using the exact code above copied and pasted. Which versions of PHP and MySQL is the server running?

The syntax is fine. I was able to create the table using the exact code above copied and pasted. Which versions of PHP and MySQL is the server running?

I am currently using MySQL 5.1 and PHP 5.5

The sql is creating the table, but why are you using $ in the table name? Maybe you want to do this:

<?php
     $sql = "CREATE TABLE ".$uID."
    (
    Col0 int(10) NOT NULL auto_increment,
    Col1 varchar(255) NOT NULL,
    Col2 varchar(255) NOT NULL,
    Col3 varchar(255) NOT NULL,
    PRIMARY KEY(Col0)
    )";


?

catalinetu, I still get the same syntax error after trying out your solution. :( .

Try this:

$sql = "CREATE TABLE $uID (".
                "Col0 INT NOT NULL AUTO_INCREMENT, ".
                "PRIMARY KEY(Col0), ".
                "Col1 VARCHAR(255) NOT NULL, ".
                "Col2 VARCHAR(255) NOT NULL, ".
                "Col3 VARCHAR(255) NOT NULL);";

The sql is creating the table, but why are you using $ in the table name?

Using variable names to create tables is a great way/sometimes needed to create dynamic tables. Also, he's using a variable name within double quotes which PHP allows. That's not what's causing this issue.

try

$sql = "CREATE TABLE $uID(".
"Col0 int(10) NOT NULL auto_increment,".
"Col1 varchar(255) NOT NULL,".
"Col2 varchar(255) NOT NULL,".
"Col3 varchar(255) NOT NULL,".
"PRIMARY KEY(Col0)".
")";       

veedeoo is correct. A mysql statement my only contain one line even when it is conjoint like veedeoo did in his example. So in short mysql under the php api will not accept multiple line query's and will only accept single line query's. Fix that part of your code and you should be done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.