1

I am trying to write a CSV in Codeignter for a result set fetched from db.

I am already using the solution mentioned here. Reports in Codeigniter

But the issue is that it writes the whole table. Now I want to fetch and write specific records. So here is my model code.

    $where = "hangtag_spoc_number = 2202";
    $result = $this->db->select('*')
            ->from('hangtag_request')
            ->where($where)
            ->get()
            ->result_array();
    return $result;

It gives me this error.

You must submit a valid result object

If I change the model code to this

    return $query = $this->db->get('hangtag_request');

It works perfectly. Is there any way I can make my model code to return the results in the form of DB object? Coz it seems thats the form we need.

Thanks.

Community
  • 1
  • 1
Omicans
  • 531
  • 1
  • 8
  • 26

2 Answers2

1

I got it solved.

Just had to remove result_array() so the correct code becomes

$where = "hangtag_spoc_number = 2202";
$result = $this->db->select('*')
        ->from('hangtag_request')
        ->where($where)
        ->get();
return $result;
Omicans
  • 531
  • 1
  • 8
  • 26
1

If you need result object then you shouldn't use the result_array()

$this->db->get();

The above statement runs the selection query and returns the result.It does return in form that can be used for presentation.

result_array()

This function returns the query result as an array, or an empty array when no result is produced.

Its good to use return $query = $this->db->get('hangtag_request'); for obtaining result object.

Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41