Search code examples
phpcodeigniterselectactiverecordsum

My select sum query returns 0 even though I have values in my database


    $this->db->select_sum('quantity');
    $query = $this->db->get('products');
    return $query->result();

I don't get it why my code returns a value of 0 even though my column has values in it and when I try putting price instead of quantity it works.

Database structure:

Name              Type               Null        Default 
price             decimal(25,4)       No          None
quantity          decimal(15,4)       Yes         0.0000

Database:

id         name          price          quantity
1          Sample1      10.0000          0.0000
2          Sample2      0.0000           45.0000

Solution

  • You can try this code.

    $this->db->select_sum('quantity', 'qty');
    $query = $this->db->get('products');
    return $query->row();
    

    Add this to your views.

    $total_quantity = $this->yourmodel->modelfunction()->qty;
    

    If still can't run, you need to provide more data for us like your views and table examples. You need to give more data for ask, if you won't others see your data. You can give an example with same data.