Page 1 of 1

MySQL syntax error?

Posted: Tue Dec 23, 2008 9:47 pm
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.'\\'';

Re: MySQL syntax error?

Posted: Tue Dec 23, 2008 10:02 pm
by Syntac
"Key" is a MySQL keyword. Put backticks around it (hit the ~ thing).

Re: MySQL syntax error?

Posted: Tue Dec 23, 2008 10:03 pm
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.

Re: MySQL syntax error?

Posted: Tue Dec 23, 2008 10:19 pm
by Cirdan
Thanks for the help!