来自ms sql的值在php中减去时会出错

时间:2017-08-10 16:39:03

标签: php sql-server

我尝试减去从ms sql server获取的两个值,但是我收到了错误。

以下是我的询问:

$amtdue = "select sum(case  when principal < 0 then principal else 0 end) as overdue, sum(case  when principal >= 0
                            then principal else 0 end) as overdue2  from loanhist where ac_no='$id' and trx_date <='$date' group by ac_no";
                            $amt = sqlsrv_query($conn, $amtdue);
                            $aod = sqlsrv_fetch_array($amt, SQLSRV_FETCH_ASSOC);

                            $amtdues = "select loan_amt from loans where ac_no='$id'";
                            $amts = sqlsrv_query($conn, $amtdues);
                            $lo = sqlsrv_fetch_array($amts, SQLSRV_FETCH_ASSOC);

以下是值:

                              $m = $lo['loan_amt']; //$m = 100000
                              $p = $aod['overdue']; //$p = -169172
                             $su = substr($p,1);
                             echo $su - $p;

当我从$p中减去$m时,我正在返回$m,这是169172.有人可以指出我正确的方向。

2 个答案:

答案 0 :(得分:0)

您没有从$ m 中减去 $ p!你在做什么:

$m = 100000; //number
$p = -169172;  //number
$su = substr($p,1); //now $su is 169172
echo $su - $p; // 169172 -(-169172)
//which is 338344

你应该做的是(如果我理解正确的话)

$m = 100000; //number
$p = -169172;  //number
$su = substr($p,1); //now $su is 169172
echo $su - $m; // 169172 - 100000
//which is 69172

答案 1 :(得分:0)

您的最后一行代码:

echo $su - $p;

没有做你打算做的事(p - m)

应该是:

echo $su - $m; 

如果仍然输出0,我建议$ m的SQL语句返回0.