if/else mysql statement

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
tarja311
Forum Commoner
Posts: 73
Joined: Fri Oct 20, 2006 10:57 pm

if/else mysql statement

Post by tarja311 »

Hi all.

I am a bit unfamilar with IF / ELSE statements in MySQL ( apparently ) Basically what i am trying to do is if the member's value is 1, set it to 0... else set it back to 1. i know my code is not the most efficient in the world ( or correct for that matter ) but can anybody help?

this is the code for a hyperlink when i click on their username.

Code: Select all

if($cmd == "member")	
{	
	mysql_query("IF `member` = '0' THEN ".
		"UPDATE `TABLE1` SET `member` = '1' WHERE `member` = '0' ".
	"ELSE ".
		"UPDATE `TABLE1` SET `member` = '0' WHERE `member` = '1' ".
	"")or die(mysql_error());
		
	echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=?show=mypage'>";
} // end if.

the error i receive :
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 'IF `member` = '0' THEN UPDATE `TABLE1` SET `member' at line 1
thanks

-- tarja
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

try this..

Code: Select all

if($cmd == "member")   
{       
        mysql_query("IF `member` = '0' THEN ".
                "UPDATE `TABLE1` SET `member` = '1' WHERE `member` = '0' "."") or die(mysql_error());
} else {
                "UPDATE `TABLE1` SET `member` = '0' WHERE `member` = '1' "."") or die(mysql_error());
               
        echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=?show=mypage'>";
} // end if.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

IF is not the way to begin a query.

The logic would be in the middle of the UPDATE.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Maybe set it to the ABS() value of `member`-1 ?
Post Reply