Hello everyone.
I have a ? on summing through a loop
MYSQL table with examples:
Table users:
id: username:
12 Bob
14 Jill
15 Psycho
Table game_log:
id: user_id: score:
1 12 -200
2 14 36
3 14 356
4 12 -456
5 15 40
As you can see:
Bob has -656 points (he sucks).
Jill has 392 points.
Psycho has 40 points.
what i'm tring to do is get the sum of each users score through a loop.
The script below will echo,
Bob -656
Bob -656
Jill 392
Jill 392
Pyscho 40
I just want it to echo each user and total score once.
Bob -656
Jill 392
Pyscho 40
My Query:
$result = mysql_query("SELECT user_id
FROM game_log");
while($row=mysql_fetch_array($result))
{
$result2 = mysql_query("SELECT SUM (score) AS total FROM game_log
WHERE id='$row[user_id]'");
while($row2=mysql_fetch_array($result2))
{
$result3 = mysql_query("SELECT username FROM users
WHERE id='$row[user_id]'");
while($row3=mysql_fetch_array($result3))
{
?>
<?=$row3[username]?><?=$row2[total]?>
<?
}
}
}
mysql_free_result($result);
Any help is greatly appreciated.
Thanks,
Dave