【问题标题】:codeigniter: fetch records and sum using SUM()codeigniter:使用 SUM() 获取记录并求和
【发布时间】:2017-06-27 16:21:26
【问题描述】:

我需要你的帮助。我想使用 codeigniter 获取记录和求和

这是我的模型`

{
     $query = $this->db->query('SELECT Sum(dfp_oral_qty) FROM daily_family_planning_register WHERE YEAR(dfp_date) = (YEAR(NOW()))');
    echo ($query->result_array());
}

`

这是我的控制器

$data['count_oral_pills_this_year'] = $this->referral->count_oral_pills_this_year();

这是我的看法

<td>
    <?php echo $count_oral_pills_this_year; ?></td> -->
</tr>

当我运行它时,我得到一个空结果,而它应该显示 300。

谢谢

【问题讨论】:

    标签: php codeigniter mysqli


    【解决方案1】:

    您的结果是一个数组,因此您应该以数组的形式访问您的结果。

    像这样..

    $rs = $this->referral->count_oral_pills_this_year();
    $data['count_oral_pills_this_year'] = $rs[0]['Sum(dfp_oral_qty)']
    

    或者你可以给列取别名,像这样

    型号

    $query = $this->db->query('SELECT Sum(dfp_oral_qty) AS total FROM daily_family_planning_register WHERE YEAR(dfp_date) = (YEAR(NOW()))');
    

    控制器

    $rs = $this->referral->count_oral_pills_this_year();
    $data['count_oral_pills_this_year'] = $rs[0]['total']
    

    注意:您使用的是“resulte_array()”,因此您必须使用特定的索引才能访问您的结果。

    如果您只查询 1 行,我建议您使用 row_array()。

    型号

    $query = $this->db->query('SELECT Sum(dfp_oral_qty) AS total FROM daily_family_planning_register WHERE YEAR(dfp_date) = (YEAR(NOW()))');
    $rs = $query->row_array();
    return $rs['total'];
    

    控制器

    $data['count_oral_pills_this_year'] = $this->referral->count_oral_pills_this_year();
    

    对不起,我的英语不好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      相关资源
      最近更新 更多