I'm completely a newbie and I have this problem drilling into my head. Any fast help will be highly appriciated.
I have a table "peoplebase" and there's a column "alias_switch". Now I want a script to act as a switch, if "alias_switch" is "1" then it should make it "2", if "2" it should make it "3" and if it is "3", it should revert it back to "1". I'm doing it in PHP. Please help me. I'm using the following code but it is not working properly. It counts till '2' and sticks.

$result = mysql_query("SELECT alias_switch FROM peoplebase");
$n = mysql_fetch_assoc($result);
if ($n = "1"){
    mysql_query("UPDATE peoplebase SET alias_switch='2'");
    }
else if ($n = "2") {
    mysql_query("UPDATE peoplebase SET alias_switch='3'");
    }
else if ($n = "3") {
    mysql_query("UPDATE peoplebase SET alias_switch='1'");
    }

Please help me with it fast. Thank you all in advance.

"x equals x" is written with 2 times an "=", not one time ;). You are now setting a new value for $n in each if() statement. You should write it as $n == 3 instead of $n = 3.

Also you're not comparing your results correctly. In line 2 of the script you present, you set $n to be a result set, not the actual result. The actual result would be $n['alias_switch'].

Try this code, if you wish:

<?php
$result = mysql_query("SELECT alias_switch FROM peoplebase");
$n = mysql_fetch_assoc($result);
$alias_switch = $n['alias_switch'];

if($alias_switch == "1") {
    mysql_query("UPDATE peoplebase SET alias_switch='2'");
}
elseif($alias_switch == "2") {
    mysql_query("UPDATE peoplebase SET alias_switch='3'");
}
elseif($alias_switch == "3") {
    mysql_query("UPDATE peoplebase SET alias_switch='1'");
}
commented: Thank you,,,, I am such a dumb ass, cant believe i was on this problem from last 1 hour. I'm so happy that i want to kiss your forehead. Thank you +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.