0

我想在total price使用时使用 MySQL 表进行计算mysql_fetch_array。但是当我 echo 时total,我会逐步得到计算出的总价:

5000
10000
16000

相反,我只想得到最终结果。

这是我的PHP代码:

$year=$_PoST['year'];
$mounth=$_POST['mounth'];

$con=mysql_connect('localhost','root','');
$select=mysql_select_db('payment');
$sql='select * from payments p 
where year(p.date) = '.$year.' and monthname(p.date) = "'.$mounth.'"';
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){
$price=$row['full_amount_must_pay'] ;

$total=$price+$total;
echo $total;

}

}

如何在没有额外两行的情况下从数据库中计算总价?

4

3 回答 3

3

首先...如果您只需要 Sum ,请像这样:

$sql='select SUM(full_amount_must_pay) from payments p where year(p.date) = '.$year.' and monthname(p.date) = "'.$mounth.'"';
$query=mysql_query($sql);
if($row=mysql_fetch_array($query)){
    echo $row[0];
}

否则在你的时间之外打印你的总数......就像这样

while($row=mysql_fetch_array($query)){
    $price=$row['full_amount_must_pay'] ;
    $total=$price+$total;
}
echo $total;
于 2012-12-14T14:07:36.117 回答
1

只用一笔?

select sum(full_amount_must_pay) from payments p
where year...
于 2012-12-14T14:07:08.977 回答
0

采用

$year=$_PoST['year'];
$mounth=$_POST['mounth'];

    $con=mysql_connect('localhost','root','');
    $select=mysql_select_db('payment');
    $sql='select * from payments p 
    where year(p.date) = '.$year.' and monthname(p.date) = "'.$mounth.'"';
    $query=mysql_query($sql);
    while($row=mysql_fetch_array($query)){
    $price=$row['full_amount_must_pay'] ;

    $total=$price+$total;


    }
    echo $total;
于 2012-12-14T14:07:02.350 回答