$total=0; //预设变量,用于存放累加结果
$sql="查询语句";
$sql=mysql_query($sql);
while($as=mysql_fetch_array($sql)){
$total=$total+$as[0];
}
还有一种办法更好,假如你要累加的字段名为 price, 则:
$sql="select sum(price) as total from 表名 where 条件";
$sql=mysql_query($sql);
$as=mysql_fetch_array($sql);
$as['total']..... //这里就是你想要的累加结果,直接让 mysql 做了,php里你就省去循环取值,估计效率好些。
$result=0;
$sql="查询语句";
$sql=mysql_query($sql);
while($as=mysql_fetch_array($sql)){
$result+=$as[0];
}
echo $result;
可以用聚合函数