Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在codeigniter中使用活动记录中的总和?_Php_Codeigniter - Fatal编程技术网

Php 如何在codeigniter中使用活动记录中的总和?

Php 如何在codeigniter中使用活动记录中的总和?,php,codeigniter,Php,Codeigniter,我想知道我的语法是否正确,这是我使用 活动记录。我想在select中汇总“金额”列,但它对我不起作用。我想知道我是否做对了?我应该分开求和部分吗 function result_getAssessment() { $this->db->select('register.regnum,register.studentid,accountspayable.accountno,accountspayable.accounttype,accountspayable.

我想知道我的语法是否正确,这是我使用 活动记录。我想在select中汇总“金额”列,但它对我不起作用。我想知道我是否做对了?我应该分开求和部分吗

function result_getAssessment()

    {
        $this->db->select('register.regnum,register.studentid,accountspayable.accountno,accountspayable.accounttype,accountspayable.amount,sum(amount) AS sum');
        $this->db->from('accountspayable');
        $this->db->join('register', 'accountspayable.regnum = register.regnum');
        $this->db->where('accountspayable.regnum','15459');
        $query=$this->db->get();
        return $query->result();


    }
我在视图中这样称呼它:

<?php foreach ($query as $row){ ?>


                   <?php echo $row->regnum;?> <br>
                   <?php echo $row->studentid;?> <br>
                   <?php echo $row->accountno;?> <br>
                   <?php echo $row->accounttype;?>  <br>
                   <?php echo $row->amount;?><br>
                   <?php echo $row->sum;?><br>


    <?php } ?>








整个查询似乎是正确的,但我觉得列名不正确。不要将列命名为
sum
,而是尝试将其命名为其他名称(因为
sum
是一个内置函数),或者至少在倒勾中引用sum列,例如

function result_getAssessment()

    {
        $this->db->select('register.regnum,register.studentid,accountspayable.accountno,accountspayable.accounttype,accountspayable.amount,sum(accountspayable.amount) AS `sum`');
        $this->db->from('accountspayable');
        $this->db->join('register', 'accountspayable.regnum = register.regnum');
        $this->db->where('accountspayable.regnum','15459');
        $query=$this->db->get();
        return $query->result();


    }
如果您提供所获得的输出,那么调试将更容易,但我的直觉告诉我问题就在列名中


编辑:看来你总结的专栏也不对。请参阅更新的代码。

您的语法正确,但流程错误。通过这个过程,你不会达到你想做的事情。echo$this->db->last_query(),然后在mysql中执行您的查询,看看结果是什么。@Vinie为什么您能说我的过程是错误的?在哪一部分?您遇到了什么问题?请您解释一下,然后我会说您的过程是否错误?实际上它告诉我,
致命错误:对非对象调用成员函数result()
我总结的列是正确的。我想把总数加起来