如何在php中加总值

时间:2013-12-18 11:32:22

标签: php mysql sql

它的基本产品系统,我正在进行最后一步检查,最后我必须显示总节省 我试图从数据库中获取一个值,如下所示

    <?php
include 'umarfarooq.php';

$s=mysql_query("SELECT * FROM `cart` WHERE `user-id`='$userid'");
while($abc=mysql_fetch_array($s))
{
$quan=$abc['quantity'];
    $bookid=$abc['book-id'];


$chktemp=mysql_query("SELECT * FROM `tampbook` WHERE `book-id`='$bookid'");
while($dtype=mysql_fetch_array($chktemp))
{
    $type=$dtype['producttype'];

}

if($type=='book')
{
    $sd=mysql_query("SELECT * from `book` WHERE `book-id`='$bookidfarooq'");
    while($umar=mysql_fetch_array($sd))
    {
        $save=$umar['save'];
        $save=$save*$quan;

        $tot=$tot+$save;

    }
}
else
{

    $sd=mysql_query("SELECT * from `journal` WHERE `book-id`='$bookid'");
    while($umar=mysql_fetch_array($sd))
    {
        $save2=$['save'];
        $save2=$save2*$quanumar;
        $tot2=$tot2+$save2;
    }
}
}


echo $tot+$tot2;



?>

输出

18010

这是错误的,因为正确的输出是

190

那么问题是什么?

1 个答案:

答案 0 :(得分:-1)

像这样改变。我认为你的总变量包含字符串类型。 floatval将字符串转换为float http://www.php.net/manual/en/function.floatval.php

echo floatval($totumar)+floatval($totumar2);