What on earth makes you think you can slap a For/Next loop within a mySQL query? 🙂
I think that's the best query I ever seen! Hehe.
$Qstring="Select * from score_history"
while ($team = mysql_fetch_array($Qstring))
{
echo $team[0] . "'s total is " . array_sum($team);
}
This will print on screen all the team names and the total of all races. The array_sum command ignores all non-number variables, which is why it's ok to add them even though the teamname is there. If you have other fields in the table that are numerical, this will not work however.
Your database design is flawed though, and if you really want to make it easy, consider this:
a table called "raceresults"
with fields
teamname, racenumber, score
With a table like this, you could do all kinds of fun queries including what you are looking for:
SELECT teamname, COUNT(racenumber) as numraces, SUM(score) AS total FROM raceresults GROUP BY teamname ORDERBY avescore DESC
This would yield a table like:
team1, 4, 30
team2, 4, 27
team3, 4, 26
you get the idea..
mySQL has a GREAT set of functions for working with results, but the design of your databases have to be able to make use of them.