Jump to content

[SOLVED] mysql table create error


brem13

Recommended Posts

hey, im trying to run a script that creates a table in a database. it gives me an error and i dont know why, the script is fine, i have another one thatis laid out the same way and that works fine.

 

mysql_query("CREATE TABLE $username1(

from VARCHAR(50),

message TEXT,

date VARCHAR(50),

time VARCHAR(50),

dt VARCHAR(50),

dt2 VARCHAR(50))")

or die(mysql_error());

 

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 'from VARCHAR(50), message TEXT, date VARCHAR(50), time VARCHAR(50), dt VARCH' at line 2

Link to comment
https://forums.phpfreaks.com/topic/148984-solved-mysql-table-create-error/
Share on other sites

from is a mysql reserved word. Wrap your field names in backticks and your query should run without errors.

 

mysql_query("CREATE TABLE $username1(
`from` VARCHAR(50),
`message` TEXT,
`date` VARCHAR(50),
`time` VARCHAR(50),
`dt` VARCHAR(50),
`dt2` VARCHAR(50))")
or die(mysql_error());

"from" is a keyword in MySQL. dont use it as a field. If u insist, try using it with backticks. but u will have to do this everytime you refer to that field.

 

I prefer this..

mysql_query("CREATE TABLE $username1(
message_from VARCHAR(50), 
message TEXT, 
date VARCHAR(50),
time VARCHAR(50),
dt VARCHAR(50),
dt2 VARCHAR(50))")
or die(mysql_error()); 

 

But this will also work.

mysql_query("CREATE TABLE $username1(
`from` VARCHAR(50), 
`message` TEXT, 
`date` VARCHAR(50),
`time` VARCHAR(50),
`dt` VARCHAR(50),
`dt2` VARCHAR(50))")
or die(mysql_error()); 

 

 

beaten by wildteen :)

 

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 ''from' VARCHAR(50), 'message' TEXT, 'date' VARCHAR(50), 'time' VARCHAR(50), ' at line 2

 

after the change, still giving error

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.