phpmysqlcodeigniterparametersquery-builder

Column nominated as second parameter of CodeIgniter's select() is not included in the result set


I am trying to fetch data from 2 tables with a JOIN query using CodeIgniter's query builder. Here I have 2 columns from 2 tables with same column name, so I define an alias.

This is my query:

public function get_all_expenses()
{
    $this->db->select("*", 'category.name as cat_name');
    $this->db->from('expense');
    $this->db->join('category', 'expense.cat_id = category.id');
    $this->db->join('users', 'expense.user_id = users.id');
    $query = $this->db->get();
    return $query;
}

I can fetch all of the data from all of the columns, but there is no cat_name element in the result set and the category.name value is lost to the column name collision.

enter image description here


Solution

  • According to the CodeIgniter documentation the database select method accept a single argument. The correct syntax for the select is then:

    $this->db->select('*, category.name as cat_name');