php and mysql wierd error

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

php and mysql wierd error

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply