unique

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

unique

Post 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?
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

it is a reserved word

Post by yacahuma »

you cant use unique. It is a reserved word in mysql


http://www.htmlite.com/mysql002a.php
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

There is no way to override it or escape it? bleh
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

Thank you, that is exactly what I was looking for.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

good trick

Post by yacahuma »

good trick but I still think is not a good idea to use reserved words
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

yacahuma, you like to think that everything is bad approach :)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: good trick

Post 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."
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

dont get me wrong

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: dont get me wrong

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