j33baS Posted March 11, 2008 Share Posted March 11, 2008 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/95589-performing-addition-to-returned-results/ Share on other sites More sharing options...
Baabu Posted March 11, 2008 Share Posted March 11, 2008 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/95589-performing-addition-to-returned-results/#findComment-489341 Share on other sites More sharing options...
j33baS Posted March 11, 2008 Author Share Posted March 11, 2008 awesome ! Thanks man Quote Link to comment https://forums.phpfreaks.com/topic/95589-performing-addition-to-returned-results/#findComment-489345 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.