Page 1 of 1

if/else mysql statement

Posted: Thu Feb 08, 2007 10:54 pm
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

Posted: Thu Feb 08, 2007 11:33 pm
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.

Posted: Thu Feb 08, 2007 11:38 pm
by feyd
IF is not the way to begin a query.

The logic would be in the middle of the UPDATE.

Posted: Fri Feb 09, 2007 3:03 am
by Kieran Huggins
Maybe set it to the ABS() value of `member`-1 ?