Rafee
Rafee

Reputation: 4078

php mysql error for creating a table

I got an error while creating a table in php with mysql database, and I tried testing directly on mysql query engine it works fine. whereas in php code it gives below error

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.

Below is the code I am writing

$query14 = mysql_query("create table $tablename (
                                           project_id INT,
                                           project_client_id INT, 
                                           project_partner_id INT, 
                                           project_manager_id INT,
                                           project_employees INT, 
                                           project_name VARCHAR(500), 
                                           project_status TEXT,
                                           project_summary LONGTEXT, 
                                           project_order INT, 
                                           project_start_date DATETIME,
                                           project_end_date DATETIME
                                         ) ENGINE = INNODB;");

and below is the image attached and table structure should be and this is the sample table i create using with phpmyadmin interface. And below is the full error

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 ''8' (project_id INT, project_client_id INT, project_partner_id INT, project_mana' at line 1`

enter image description here

Upvotes: 0

Views: 1186

Answers (5)

Ariful Islam
Ariful Islam

Reputation: 7675

I didn't see any problem with your query. But what is the value of $tablename? Two error possibilities here :

  1. The variable $tablename is empty.
  2. $tablename is a key-word.

Please check the above two otherwise its all right.

UPDATE :

As per your updated question please try with the following code. I think it will help you.

$query14 = mysql_query("create table `$tablename` (
                                           `project_id` INT,
                                           `project_client_id` INT, 
                                           `project_partner_id` INT, 
                                           `project_manager_id` INT,
                                           `project_employees` INT, 
                                           `project_name` VARCHAR(500), 
                                           `project_status` TEXT,
                                           `project_summary` LONGTEXT, 
                                           `project_order` INT, 
                                           `project_start_date` DATETIME,
                                           `project_end_date` DATETIME
                                         ) ENGINE = INNODB;");

Upvotes: 4

ksg91
ksg91

Reputation: 1299

The problem is, $tablename is 8 currently and for SQL table, table name must start with a letter. Current tablename is invalid.

Upvotes: 0

jogesh_pi
jogesh_pi

Reputation: 9782

As you said in above comments that after echoing the $tablename you got the value 8 then it is not possible to create table.

you have to rename the value of $tablename like tbl_8.

remember with the name tbl-8 your also got error,,

Upvotes: 0

viyyer
viyyer

Reputation: 146

I think the issue is what is getting replaced for $tablename.

Having the full code example would be more useful in debugging the error.

The mysql create statement works just fine here on mysql 5.1.58 .

Upvotes: 0

yrosen
yrosen

Reputation: 364

Your query syntax is fine, it looks like $tablename is empty.

Upvotes: 1

Related Questions