Ok here is the code. `

mysql_query("UPDATE `special` SET `Counter` = `Counter` - 1");
$Counter = mysql_query("SELECT Counter FROM special where Id = 1");
if($Counter == 0)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=winner.php\">";

mysql_query("UPDATE `special` SET `Counter` = `Counter` + 500");
}

else

{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}

?>

im attempting to create a counter that starts at 500 and every visit it counts down by 1 till it hits 0 when it does it should go to the winner.php page and reset the counter to 500.

What is happening is it hits zero and never goes to winner.php or resets the counter. where am I messing up? It goes to the index page just fine. although i guess i have extra code to go to the index page. this coding will be called from the index so can i have a else do nothing? such as else {}.

Member Avatar for iamthwee

isn't your logic mixed up?

Member Avatar for iamthwee

isn't your logic mixed up?

how so?

is happening is it hits zero and never goes to winner.ph

It is not going to winner.php because mysql_query will not gives you the resultset and not the count. If you need count then do as following

$resultSet = mysql_query("YOUR QUERY COMES HERE");
$counter = mysql_num_rows($resultSet); // Now your counter variable will have value
if($counter == 0)
.
..
....

$counter = mysql_num_rows($resultSet);

well now it goes to winner.php and resets the counter, however it keeps going to the winner page...

Member Avatar for iamthwee

Actually... hmm

here is what i have now....

mysql_query("UPDATE `special` SET `Counter` = `Counter` - 1");
$resultSet = mysql_query("SELECT Counter FROM special where Id = 1");
$Counter = mysql_num_rows($resultSet);
if($Counter == 0)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=winner.php\">";

mysql_query("UPDATE `special` SET `Counter` = `Counter` + 500");
}

else

{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}

?>

and now again it hits zero and does not go to the winner page or update the counter. So its not reading the ounter value again.

Member Avatar for Zagga

Hi azgold,

try:

$counterValue = mysql_query("SELECT Counter FROM special where Id = '1'");
$newCounterValue = $counterValue -1;

if ($counterValue == 0){
    mysql_query("UPDATE special SET Counter='500'");
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=winner.php\">";
} else {
    mysql_query("UPDATE special SET Counter='$newCounterValue'");
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
?>

In your original post you asked if you could have an empty ELSE statement. The ELSE clause is optional, so yes you can leave it out if you want.

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.