0

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 Answers 3

1

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();
    }
Sign up to request clarification or add additional context in comments.

3 Comments

hello, thanks for answering, but its already set to 'postgre', and all other controller work fine for me, maybe it some kind of issue with query ...
@user1218948,The result of query was empty?Could you check my updated answer
it returns Fatal error: Call to a member function num_rows() on a non-object in
0

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();

8 Comments

it return Fatal error: Call to a member function result() on a non-object in, in MYSQL it work fine, but in PostGreSQL it return same...
try to print last query ... using this $this->db->group_by('ci_categories.cat_id')->get(); and after this line echo $this->db->last_query();
SELECT "ci_categories"."cat_id", "ci_categories"."cat_name", COUNT(ci_albums.album_id) AS total_albums FROM "ci_categories" LEFT JOIN "ci_albums" ON "ci_albums"."cat_id" = "ci_categories"."cat_id" GROUP BY "ci_categories"."cat_id"
try using return $this->db->group_by('ci_categories.cat_id')->get()->result_array();
Fatal error: Call to a member function result_array() on a non-object in
|
0

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

2 Comments

it return Message: Invalid argument supplied for foreach(); it mean, we get FALSE from model...
the return array() instead of false return ($query)?$query->result():array();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.