0

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.

mauris
  • 42,982
  • 15
  • 99
  • 131
Phil Jackson
  • 10,238
  • 23
  • 96
  • 130

2 Answers2

4

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' )
mauris
  • 42,982
  • 15
  • 99
  • 131
Jarod Elliott
  • 15,460
  • 3
  • 35
  • 34
  • thank you! How come you have to enclose all in backquotes tho? – Phil Jackson Apr 11 '10 at 08:39
  • 1
    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 Apr 11 '10 at 08:42
  • 1
    It's just how you should generally write your queries. Best practice. – jayarjo Apr 11 '10 at 08:43
  • Thank you, never wrote queries with backquotes around the column name but will do from now on. – Phil Jackson Apr 11 '10 at 08:56
1

Isn't FROM a reserved word in MySQL?

The Disintegrator
  • 4,147
  • 9
  • 35
  • 43