MySQL syntax 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
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

MySQL syntax error?

Post by Cirdan »

I'm getting this error, and have no idea why
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 'key='e6b937ccb04627ca9ce04fc06b4c550b' WHERE username='test'' at line 1
On this statement
UPDATE r_users SET key='e6b937ccb04627ca9ce04fc06b4c550b' WHERE username='test'

Code: Select all

$query = 'UPDATE r_users SET key=\\''.$key.'\\' WHERE username=\\''.$username.'\\'';
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: MySQL syntax error?

Post by Syntac »

"Key" is a MySQL keyword. Put backticks around it (hit the ~ thing).
Last edited by Syntac on Tue Dec 23, 2008 10:03 pm, edited 1 time in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: MySQL syntax error?

Post by califdon »

"Key" is a reserved word in MySQL. You should not use it as a field name. Ref.: http://www.hockinson.com/index.php?s=147. You actually can make it work by enclosing it in back-ticks:

Code: Select all

`key`
but it's still not recommended.

Also, you are doing it the hard way, using all those single quotation marks and escape backslashes. Why not do it the easy way?

Code: Select all

$query = "UPDATE r_users SET `key`='$key' WHERE username='$username'";
 
As long as you're only using simple PHP $ variables, they will be interpreted within a double-quotes string.
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: MySQL syntax error?

Post by Cirdan »

Thanks for the help!
Post Reply