Small SQL problem

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
iKevz
Forum Newbie
Posts: 4
Joined: Wed Aug 17, 2011 3:13 pm

Small SQL problem

Post by iKevz »

Hey, could anybody tell me whats wrong with this sql? I know it's just a tiny error and i'm gunna kick myself after but i've wrote so much code today i just can't find it

Code: Select all

 $result = ("UPDATE users SET 'about' = '$about', 'interested' = '$interested', 'relationship' = '$relationship', 'gender' = '$gender' WHERE username = '$user'");
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Small SQL problem

Post by twinedev »

You have the fields surrounded by single quotes ( ' ) instead of backticks ( ` ):

Code: Select all

$result = ("UPDATE users SET `about` = '$about', `interested` = '$interested', `relationship` = '$relationship', `gender` = '$gender' WHERE `username` = '$user'");
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Small SQL problem

Post by phphelpme »

This is how I would normally code it:

Code: Select all

$result = "UPDATE users SET about='$about', interested='$interested', relationship='$relationship', gender='$gender' WHERE username='$user'";
Best wishes
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: Small SQL problem

Post by genix2011 »

Hi,

what twindev did is better, because it is the mysql standard and you won't get in trouble if you use reserved words as field names.

Greets.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Small SQL problem

Post by phphelpme »

That is a fair statement I agree but lets face it, you should not use reserved words anyway because they are reserved. :)

Best wishes
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: Small SQL problem

Post by genix2011 »

Yeah you are right, but what if you wrote your query for lets say MySQL 4 and now tries to execute it in MySQL 5, now there could be new reserved words in MySQL 5 that were not in MySQL 4.
It just feels safer and better to use backticks :-)
Last edited by genix2011 on Fri Aug 19, 2011 3:19 pm, edited 1 time in total.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Small SQL problem

Post by phphelpme »

Yes, I do agree with you on that one and I can not say anything negative about your statements. It would be safer to use those back ticks but I would not understand anyone needing to revert back to 'v4' because as standard servers come with the latest version and as soon as another 'v' is available the hosting providers upgrade but always add some code to support backwards capability.

But in the safe run i suppose you might as well use them but to me on my personal servers and remote servers I would not use anything other than the latest version I suppose. lol :)

Best wishes
Post Reply