Page 1 of 1

unique

Posted: Thu Jul 26, 2007 9:59 pm
by SidewinderX
I'm getting this error:
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 'unique='1' WHERE cid='3'' at line 1
It is refering to this line in my PHP

Code: Select all

$sql = mysql_query("UPDATE tl_stats SET unique='$unique' WHERE cid='$cid'") or die("Error: ".mysql_error());
The column `unique` DOES exist. If I change the name of the column [to say `test`], this query works, but it does not work when the column name is `unique`. Is unique some kind of keyword and this error is a result of that?

it is a reserved word

Posted: Thu Jul 26, 2007 10:03 pm
by yacahuma
you cant use unique. It is a reserved word in mysql


http://www.htmlite.com/mysql002a.php

Posted: Thu Jul 26, 2007 10:14 pm
by SidewinderX
There is no way to override it or escape it? bleh

Posted: Thu Jul 26, 2007 10:22 pm
by John Cartwright
SidewinderX wrote:There is no way to override it or escape it? bleh
It is good practice to wrap all table and column names in backticks, e.g. `unique` to avoid running into such conflicts :wink:

Posted: Thu Jul 26, 2007 10:47 pm
by SidewinderX
Thank you, that is exactly what I was looking for.

good trick

Posted: Fri Jul 27, 2007 7:00 am
by yacahuma
good trick but I still think is not a good idea to use reserved words

Posted: Fri Jul 27, 2007 7:04 am
by miro_igov
yacahuma, you like to think that everything is bad approach :)

Re: good trick

Posted: Fri Jul 27, 2007 9:07 am
by superdezign
yacahuma wrote:good trick but I still think is not a good idea to use reserved words
It's perfectly fine to use reserved words for table and column names. Some of them are very descriptive and are exactly what we're looking for. Just because MySQL found them descriptive as well doesn't mean that other's should be banned from their use. A prime example of this is "date."

dont get me wrong

Posted: Fri Jul 27, 2007 9:56 am
by yacahuma
Please dont get me wrong. You have a really good argument. some tutorials say to ad a small description to fields in a table like

ma_user
ma_fullname
ma_etcetc

I just have my style and things that work for me.

If that solutions works and is maintainable (will not cause trouble for the next programmer) , use it.

Re: dont get me wrong

Posted: Fri Jul 27, 2007 10:06 am
by superdezign
yacahuma wrote:Please dont get me wrong. You have a really good argument. some tutorials say to ad a small description to fields in a table like

ma_user
ma_fullname
ma_etcetc

I just have my style and things that work for me.

If that solutions works and is maintainable (will not cause trouble for the next programmer) , use it.
Oh no, I agree, but it isn't due to MySQL reserved words, but to not having obvious column names. Just an thin extra layer of security.