Page 1 of 1

Select query through a loop

Posted: Sun Jan 18, 2009 7:20 pm
by Shintei
I apologize in advance if this seems like a simple problem, if it has been asked many times before or if the title is vague but I can't seem to figure out the correct syntax.

Using PhP 4.4.9 and MySQL 5.0.67 if that helps.

What I am trying to do is perform a select from the table for the building levels. There isn't an exact amount of buildings in the table since it can range anywhere from none to about 30 of them per account. Then for each building, figure out what level the building is (anywhere from 1 to 5) and add a certain amount of points for each one.

This code hasn't generated an error, but it doesnt do what I am trying to do. I'v tried using mysql_fetch_assoc, mysql_fetch_array and so forth but I'm not sure if any of it is correct.

Code: Select all

$buildingSelect = sprintf("SELECT buildingLevel FROM building WHERE loginName='%s'", mysql_real_escape_string($_SESSION['loginname']));
$buildingQuery = mysql_query($buildingSelect, $cxn);
 
while ($checkForTotal = mysql_fetch_assoc($buildingQuery))
{
    if ($checkForTotal == 1)
    {
    $AdditonalPointTotal +=2;
    }
    else if ($checkForTotal == 2)
    {
    $AdditonalPointTotal +=6;
    }
    else if ($checkForTotal == 3)
    {
    $AdditonalPointTotal +=12;
    }
    else if ($checkForTotal == 4)
    {
    $AdditonalPointTotal +=24;
    }
    else if ($checkForTotal == 5)
    {
    $AdditonalPointTotal +=48;
    }
 
}

Re: Select query through a loop

Posted: Sun Jan 18, 2009 8:08 pm
by Burrito
$checkForTotal is an associative array therefore you need to check it's key value...not the value of the array itself.

try using $checkForTotal['buildingLevel'] instead.