Jump to content

Performing addition to returned results ?


j33baS

Recommended Posts

Hi i've been searching around for a way to perform an addition to all the reults from a while loop but cant really crack it and was wondering if anyone could point me in the right direction ! If i have a table like this ...

 

SCORES (playername, score)

Player1 - 3

Player1 - 5

Player1 - 1

Player1 - 6

Player2 - 2

Player2 - 3

 

and a query like this ...

 

<?php
$sql = "SELECT * FROM scores where playername = $playername";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){
echo "playername and score...";
}
?>

 

will output fro "player1" ...

 

Player1 - 3
Player1 - 5
Player1 - 1
Player1 - 6

 

what can i do to get all those scores added up ? so i can have ...

 

total = 15

 

ive been messing around making the "score" variable  = to a variable $x and trying to do $x++ or sumthing ? do i have to make some sort of function ? would appreciate any help, cheers !

Link to comment
https://forums.phpfreaks.com/topic/95589-performing-addition-to-returned-results/
Share on other sites

<?php
$sql = "SELECT * FROM scores where playername = $playername";
$result = mysql_query($sql);
$score=0;
while ($row = mysql_fetch_array($result)){
//do rest here 
$score+=$row["score"];//where score is the column name in the database
}
echo "Total".$score;
?>

Hope this works for you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.