简体   繁体   English

PHP中的总计

[英]grand total sum in php

this may sound lame but i am stuck up with summing up the values retrieved from mysql. 这听起来可能很but脚,但我坚持总结从mysql检索的值。 i want to sum up the grand total of the sales invoice items that is retrieved from mysql database. 我想总结从mysql数据库检索到的销售发票项目的总计。 following is the code for sub total for each line which is fine. 以下是每行总计的代码,这很好。

while($row = mysql_fetch_array($qry)){  
echo $row['b'] * $row['c'];
}

'b' is the quantity and 'c' is the price.all these values are retrieved from mysql and the no of rows is variable and all this is inside the while loop. 'b'是数量,'c'是价格。所有这些值都是从mysql检索的,行号是可变的,所有这些都在while循环内。 Now outside of the while loop, the last row, i want the grand total. 现在在while循环之外,最后一行,我想要总计。 can somebody point me in the right direction. 有人可以指出我正确的方向。 i tried 我试过了

echo SUM($row['b'] * $row['c']);

but this doesn't work and gives an error. 但这是行不通的,并且会出现错误。 i am sure i am doing it wrongly. 我确定我做错了。

$total = 0;
while($row = mysql_fetch_array($qry))
{  
  echo $row['b'] * $row['c'];
  $total += ($row['b'] * $row['c']);
}

// at the end, $total = grand total

Why not use SQL for things SQL does best? 为什么不使用SQL做SQL最好的事情?

SELECT SUM(b*c)
FROM table

Simple as that. 就那么简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM