views:

38

answers:

2

Hi all, im tearing my hair out over this one. A query is throwing an 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, SUBJECT, DATE, READ, MAIL ) VALUES ( 'EJackson', 'dfdf', '1270974101', 'fa' at line 1

I printed out the query to see what could be the problem:

INSERT INTO db.tablename ( FROM, SUBJECT, DATE, READ, MAIL ) VALUES ( 'EJackson', 'dfdf', '1270974299', 'false', 'dfdsfdsfd' )

and finaly the structure consists of:

CREATE TABLE db.tablename (
  `ID` int(12) NOT NULL auto_increment,
  `FROM` varchar(255) NOT NULL,
  `SUBJECT` varchar(255) NOT NULL,
  `DATE` varchar(255) NOT NULL,
  `READ` varchar(255) NOT NULL,
  `MAIL` varchar(255) NOT NULL,
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

I can't find anything wrong. Any help would be much appreciated.

+4  A: 

In the insert statement, "FROM" is a keyword in SQL so you need to enclose it in backquotes like you have in your create table statement.

So it'll be like:

INSERT INTO db.tablename (`FROM`, `SUBJECT`, `DATE`, `READ`, `MAIL` ) VALUES ( 'EJackson', 'dfdf', '1270974299', 'false', 'dfdsfdsfd' )
Jarod Elliott
thank you! How come you have to enclose all in backquotes tho?
Phil Jackson
Since FROM is a reserved keyword, if you use it as a column name the backquotes around it tell the parser not to recognize it as a keyword. Anything which is not a keyword doesn't need backquotes but it can just be safer to use them all the time.
Jarod Elliott
It's just how you should generally write your queries. Best practice.
jayarjo
Thank you, never wrote queries with backquotes around the column name but will do from now on.
Phil Jackson
+1  A: 

Isn't FROM a reserved word in MySQL?

The Disintegrator
redundancy.....
thephpdeveloper
When I wrote my answer, there was no other...
The Disintegrator