how to fix codeigniter non-object error in PostgreSQL database

863 views Asked by At

we're using codeigniter with postgresql, our following code works in MySQL type database, but in postgresql, it shows errror

Fatal error: Call to a member function result() on a non-object in /application/models/category_m.php on line 61

Our Model Code:

$this->db->select('ci_categories.cat_id, ci_categories.cat_name, COUNT(ci_albums.album_id) AS total_albums');
$this->db->from('ci_categories');
$this->db->join('ci_albums', 'ci_albums.cat_id = ci_categories.cat_id', 'left');

return $this->db->group_by('ci_categories.cat_id')->get()->result();
3

There are 3 answers

3
Shijin TR On

Set dbdriver in application/config/database.php

  $db['default']['dbdriver'] = "postgres";

The error is likely due to the fact that the query (and resulting result() object) are empty. Ensure that the query actually returns at least one record before attempting to use the result() object:

   $this->db->select('ci_categories.cat_id, ci_categories.cat_name, COUNT(ci_albums.album_id) AS total_albums');
   $this->db->from('ci_categories');
   $this->db->join('ci_albums', 'ci_albums.cat_id = ci_categories.cat_id', 'left');

   $query=$this->db->group_by('ci_categories.cat_id')->get();
    if($query->num_rows() > 0){
      return $query->result();
    }
2
Fisherman On

try this on the last line --

$query  = $this->db->group_by('ci_categories.cat_id')->get();
 return ($query)?$query->result():array(); // instead of false you can return empty array if you need
8
Mohan On

try using this code

$this->db->select('ci_categories.cat_id, ci_categories.cat_name, COUNT(ci_albums.album_id) AS total_albums',FALSE);
$this->db->from('ci_categories');
$this->db->join('ci_albums', 'ci_albums.cat_id = ci_categories.cat_id', 'left');

return $this->db->group_by('ci_categories.cat_id')->get()->result();