0

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

Edited by EJL242000

5
Contributors
8
Replies
60
Views
3 Years
Discussion Span
Last Post by cwarn23
0

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?

0

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

Edited by EJL242000

0

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)
    )";


?
0

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

0

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);";
0

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.

1

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)".
")";       
1

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.