Select query through a loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Shintei
Forum Newbie
Posts: 2
Joined: Sun Oct 26, 2008 5:57 pm

Select query through a loop

Post 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;
    }
 
}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Select query through a loop

Post 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.
Post Reply