Forums

PHP

This topic is locked

Add all values of a recordset

Posted 13 Dec 2006 13:52:58
1
has voted
13 Dec 2006 13:52:58 Stephen Wright posted:
Hi there.

I am developing a php based program to invoice people, I got a really basic one for you here but I can't for the life of me remember how I did it last time!.

The recordset selects all records from an invoice table for a specific user. One of the columns is 'cost' I want to add all the values of the 'cost' column from the results in the query. How do I do this? many thanks

Stephen

Replies

Replied 19 Dec 2006 18:03:18
19 Dec 2006 18:03:18 Roddy Dairion replied:
You can use SUM()
In MySQL code is
<pre id=code><font face=courier size=2 id=code>
select SUM(costs) as `Sum` from tablename where field_user_id = user_id
</font id=code></pre id=code>
In PHP
<pre id=code><font face=courier size=2 id=code>
$sum = mysql_result(mysql_query("select SUM(costs) as `Sum` from tablename where field_user_id = $user_id", 0);
echo $sum;
</font id=code></pre id=code>

Reply to this topic