dongtang6775 2014-07-22 05:43
浏览 149
已采纳

在PDO中获取SUM

Below is my code and for some reason it's not giving me the SUM. This always returns 0. Why doesn't it work?

I've used if ($totSubmits==''){ to avoid blank fields in my database.

I also tried removing AS due_fees and using $dueAmont = $result[0], but no luck though.

$sql= "SELECT SUM(dueFees) AS due_fees FROM coursePayments WHERE studentId = $student"; 
$stmt = $c->prepare($sql);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_NUM);
$dueAmont = $result['due_fees']; 
if ($dueAmont==""){
    $dueAmont = '0';
}
echo $student." due amount ".$dueAmont;
  • 写回答

2条回答 默认 最新

  • dougu3988 2014-07-22 05:48
    关注

    Gah, use parameter binding! Also, fetchColumn() is nice and easy for single row / single column results

    $sql= "SELECT SUM(dueFees) FROM coursePayments WHERE studentId = ?"; 
    $stmt = $c->prepare($sql);
    $stmt->execute(array($student));
    $dueAmont = (int) $stmt->fetchColumn();
    

    Addendum

    When developing, always run your PHP environment with full error disclosure. Then you won't miss easy mistakes like in your code above. In php.ini

    display_errors = On
    error_reporting = E_ALL
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统