CodeIgniter Forums
Fatal error: Call to a member function result() on a non-object in - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Fatal error: Call to a member function result() on a non-object in (/thread-53942.html)



Fatal error: Call to a member function result() on a non-object in - El Forum - 08-15-2012

[eluser]Unknown[/eluser]
Hi!
I am new to CodeIgniter and I am facing a few problems here.
I get the following error:
Fatal error: Call to a member function result() on a non-object in C:\Program Files\...\application\models\GetConfig.php on line 13

The weird thing is when I refresh a few times some pages load and some don't no matter what.
Here is my GetConfig model.
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class GetConfig extends CI_Model {

function __construct()
    {
        parent::__construct();
    }

public function fetch_config()
{
  $query = $this->db->query("SELECT * FROM config LIMIT 1");
  return $query->result();
}

}

Here is my CategoryModel model.
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CategoryModel extends CI_Model {

function __construct()
    {
        parent::__construct();
    }

public function fetch_categories($page)
{
  $records = 25;
  $end = $page * $records;
  $start = ($end - $records);
  $limit = "LIMIT $start,$end";
  $query = $this->db->query("SELECT * FROM categories WHERE category_active = 1 OR category_active = 0 ORDER BY category_id DESC $limit");
  return $query->result();
}

public function get_parents($id)
{
  $original_id = $id;
  for($i=1;;$i++)
  {
   if($id >= 0)
   {
    $query = $this->db->query("SELECT * FROM categories WHERE category_id = '$id'");
  
    foreach($query->result() as $result)
    {
     $id = $result->category_parent;
     $parents[] = $this->get_name($result->category_parent);
    }
   }
   else
   {  
    break;
   }
   unset($query);
  }
  $parents = array_reverse($parents);
  $parents[] = "<strong>".$this->get_name($original_id)."</strong>";
  unset($parents[0]);
  return implode(" <strong>/</strong> ",$parents);
}

public function get_parent_ids($id)
{
  $original_id = $id;
  for($i=1;;$i++)
  {
   if($id > 0)
   {
    $query = $this->db->query("SELECT * FROM categories WHERE category_id = '$id'");
  
    foreach($query->result() as $result)
    {
     $id = $result->category_parent;
     $parents[] = $result->category_parent;
    }
   }
   else
   {  
    break;
   }
  }
  return $parents;
}

public function get_children($id)
{
  $original_id = $id;
  do
  {
   $query = $this->db->query("SELECT * FROM categories WHERE category_parent = '$id'");

   foreach($query->result() as $result)
   {
    $id = $result->category_id;
    $children[] = $result->category_id;
   }
  }
  while($query->num_rows() > 0);
  $children = array_reverse($children);
  return $children;
}
  
public function get_name($id)
{
  if($id != 0)
  {
   $query = $this->db->query("SELECT category_name FROM categories WHERE category_id = '$id'");
   $result = $query->row();
   return $result->category_name;
  }
}

public function get_status($bool)
{
  if($bool)
  {
   return "Active";
  }
  else
  {
   return "Inactive";
  }
  
}

public function get_num_pages()
{
  $query = $this->db->query("SELECT * FROM categories WHERE category_active = 1");
  return ceil($query->num_rows()/50);
}
}

Here is my controller:
Code:
class Categories extends CI_Controller {

function __construct()
        {
            parent::__construct();
     $this->load->model("admin/CategoryModel");
        }

public function index($page = 1)
{
  if($this->session->userdata("grade") != 007)
  {
   header("Location: ".base_url()."login");
  }
  /************************************************************************************************************************
  * Load Header Data
  ************************************************************************************************************************/
  $header["site_config"] = $this->GetConfig->fetch_config();
  $header["site_config"][0]->pagetitle = "Categories - Page $page : Administrator Panel";
  
  /************************************************************************************************************************
  * Load Categories
  ************************************************************************************************************************/
  $categories["categories"] = $this->CategoryModel->fetch_categories($page);
  foreach($categories["categories"] as $category)
  {
   $category->category_parent = $this->CategoryModel->get_parents($category->category_id);
   $category->category_active = $this->CategoryModel->get_status($category->category_active);
  }
  $categories["pages"] = $this->CategoryModel->get_num_pages();
  
  /************************************************************************************************************************
  * Load Footer Data
  ************************************************************************************************************************/
  $footer = "";
  
  /************************************************************************************************************************
  * Load All Views
  ************************************************************************************************************************/
  $this->load->view("admin/header",$header);
  $this->load->view("admin/categories",$categories);
  $this->load->view("admin/footer",$footer);
}

PS: I am autoloading the GetConfig model.

I noticed one more thing, when in controller I comment out this line:
$category->category_parent = $this->CategoryModel->get_parents($category->category_id);
and refresh the page for few times it loads fine.

Kindly help me find a solution.
Thank You!!



Fatal error: Call to a member function result() on a non-object in - El Forum - 08-15-2012

[eluser]Aken[/eluser]
The problem is coming from your fetch_config() method. And the error is because your $query variable is not a proper response object, so $query->result() does not exist. Since your code there looks fine, I'd guess it has something to do with your connection to the database.