adding up all the totals in an mysql table fields

A

Anonymous

Guest
Hi all,

How do I do something like adding up all the totals in an mysql table fields (i.e. number_of_sales)

So if 'user_table_cash' had a field called 'number_of_sales' and I done a query like
$query = "SELECT * FROM user_table_cash WHERE id='davetomas'";

and there was 10 results with the numbers 2,33,44,55,22,44,66,77,33,55

how would I add them all up to display the total amount?

Any help would be tops :)
 
Code:
$total = 0;
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$total = $total + $row["field name"];
}
echo "The total is: $total ";

Regards,
Andrew
 
oh, meant to say, $query is the $query that you specified.

Andrew
 
Hi Andrew thanks for the help mate :)

can you please tell me if this is the right way?

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT * FROM ua_referers WHERE id='Referafriend'";
$total = 0;
$result = mysql_query($query) or die(mysql_error());
mysql_close();

while ($row = mysql_fetch_array($result))
{
$total = $total + $row["Total"];
}
echo "The total is: $total ";

thanka agian.
 
No problem, any problems just post them here or freel free to contact me privately by any of the messengers at the bottom of this post :)

Andrew
 
Back
Top