Reputation: 53
How do I add all the amounts
together so that instead of this select script returning all the individual withdrawal amounts return one number of them all added together?
<?php
$end_date = strtotime($nextdate);
$start_date = strtotime($statement_date);
$username = $usr['username'];
$result = mysql_query("SELECT * FROM `usr_withdraw` WHERE `timestamp` > '$start_date' AND `timestamp` < '$end_date' AND `username` = '$username'")or die(mysql_error());
while($balance = mysql_fetch_array( $result )) {
echo $balance['amount'];
}
?>
Upvotes: 0
Views: 59
Reputation: 162771
To return only 1 row with the aggregate sum of all amounts use the sql sum()
function.
SELECT sum(amount) FROM `usr_withdraw` WHERE ...
Upvotes: 1
Reputation: 3723
Do
$result = mysql_query("SELECT SUM(amount) FROM `usr_withdraw` WHERE `timestamp` > '$start_date' AND `timestamp` < '$end_date' AND `username` = '$username'")or die(mysql_error());
But also keep in mind that these mysql_* functions are out of PHP from version 7 on, then your code will be soon out of date. The ideal is using mysqli_* functions or PDO.
Upvotes: 0