Page 1 of 1

php and mysql wierd error

Posted: Wed Jan 03, 2007 4:39 pm
by Citizen
I've looked up the error and checked my code 100 times and I still cant find anything to change.... any ideas?


Here's my code:

Code: Select all

$sqla="INSERT INTO `mail` (to, from, title, text, time, seen, type) VALUES ('$cname', '$rlname', '$name2', '$name', $time, 1, 'mail')";
$resulta=mysql_query($sqla) or die(mysql_error());
Echo'd $sqla:

Code: Select all

INSERT INTO `mail` (to, from, title, text, time, seen, type) VALUES ('userx', 'userb', 'test', 'test', 1167863902, 1, 'mail')
Error:
You have an error in your SQL syntax; check the manual that corresponds to yo
ur MySQL server version for the right syntax to use near 'to, from, title, text, time, seen, type) VALUES ('userx', 'userb', 'test', ' at line 1

Posted: Wed Jan 03, 2007 5:03 pm
by onion2k
`to` is a MySQL reserved word. You can either put quotes around the column names (using single quotes or backticks like I have in this post), or better yet, rename the column to something else. `time` and `text` are allowed as identifiers but they also clash with a MySQL function and a column type word.

http://dev.mysql.com/doc/refman/4.1/en/ ... words.html

Posted: Wed Jan 03, 2007 5:13 pm
by boo_lolly

Posted: Wed Jan 03, 2007 5:42 pm
by RobertGonzalez
I don't think that will help boo_lolly, seeing as his syntax was correct but he was using a MySQL reserved word for a field name.

Posted: Wed Jan 03, 2007 7:14 pm
by Citizen
Thanks guys! I'm sure I would have never found it looking around. I went through 50 google results for that error and not one had anything to do with reserved word syntaxes.

Thanks again!

Posted: Thu Jan 04, 2007 2:54 pm
by RobertGonzalez
You know, a good practice to get into is wrapping all your field names in backticks (`) so you don't run into that situation.