jesse pfieffer
jesse pfieffer

Reputation: 3

Add all numbers in row

I have a database like this:

ID    |    AMOUNT 
1           15.00
2          100.00
3          100.00

I need to add all the amounts together. I have tried some PHP math stuff but just can't make it work.

<?php
$total = mysql_query("SELECT amount FROM payments") 
or die(mysql_error()); 

$grandtotal=   
    while($total1 = mysql_fetch_array( $total )) {

$total1['amount']+


};

?>

Upvotes: 0

Views: 56

Answers (2)

anon
anon

Reputation:

There are two solutions for the specific task you're trying to accomplish:

while($total1 = mysql_fetch_array( $total )) {

$total1['amount']++; //returns Returns $total1['amount'], then increments it by one.

} // <-- semicolon removed

As Mark Baker suggested in comments, you could do it from within your SQL query using SUM:

SELECT SUM(amount) AS amount_sum FROM payments

Upvotes: 0

A.O.
A.O.

Reputation: 3763

SELECT SUM(amount) as sum_amount FROM payments

Upvotes: 1

Related Questions