Jump to content

summing results


funbinod

Recommended Posts

I've a query that gets some rows with some results. and I want to sum all the results

 

like I get $balance from a query

while ($r = $q->fetch_array) {    echo $balance.'<br />';}

and when I get the rows with $balance, I want to sum all of them and display at the bottom

I tried,

$total = 0;while ($r = $q->fetch_array) {    $total = $balance++;    echo $balance.'<br /';}echo $total;

but its not giving the required result. please guide.

 

thanks in advance....

Link to comment
Share on other sites

I also tried

$total += $balance;

and it also doesn't work.

 

actually the $balance is derived from lots of calculations after the query so I don't know how can I make the sum of $balance in sql. that's why I wish the $total calculated with php...

Link to comment
Share on other sites

I also tried$total += $balance;and it also doesn't work.

That's going to add $balance to $total. Sounds like that's what you want. I'm not sure what you're doing where that wouldn't work.

actually the $balance is derived from lots of calculations after the query so I don't know how can I make the sum of $balance in sql.

I figured, because your first code block doesn't show where you're getting $balance from, but you can do a lot of calculations with SQL.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...