EllisLab text mark
Advanced Search
     
Fatal error: Call to a member function database() on a non-object
Posted: 29 May 2012 12:31 AM
Joined: 2012-05-28
4 posts

Hello,

I got following error when I am going to use $this->load->database();. I use Eclipse as my IDE.

Fatal error: Call to a member function database() on a non-object in C:\xampp\htdocs\TestCI\application\models\news_model.php on line 7


And this is my code(../models/news_model.php)

class News_model extends CI_Model {

 
public function __construct()
 
{
  
  $this
->load->database();
 
}
 
 
public function get_news($slug FALSE)
 
{
  
if ($slug === FALSE)
  
{
   $query 
$this->db->get('news');
   return 
$query->result_array();
  
}
  
  $query 
$this->db->get_where('news', array('slug' => $slug));
  return 
$query->row_array();
 
}

 

And also I put following code in to autoload.php

$autoload['libraries'= array('database'); 


Can anyone tell how to solve this???

 
Posted: 29 May 2012 12:39 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6590 posts

Did you setup database information in ./application/config/database.php ?

 Signature 

Certified State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

Skype: insitfx

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 29 May 2012 01:08 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

You need to add parent::__construct(); as the first thing in your constructor.

 
Posted: 29 May 2012 01:13 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-05-28
4 posts

@InsiteFX- Yes, I did that.Thanks buddy smile

@Aken - I did that as u said but it not works yet. :(

 
Posted: 29 May 2012 01:14 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

Then you did it wrong. That should be the first item inside your __construct() method.

 
Posted: 29 May 2012 01:15 AM   [ # 5 ]   [ Rating: 0 ]
Joined: 2012-05-28
4 posts

I did as u said friend. But still not working…

public function __construct()
 
{
  parent
::__construct();
  
$this->load->database(); 

}

 
Posted: 29 May 2012 01:20 AM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

Is it the exact same error being displayed? Also, if you’re autoloading your database, there’s no reason to load it in your model…

 
Posted: 29 May 2012 01:22 AM   [ # 7 ]   [ Rating: 0 ]
Joined: 2012-05-28
4 posts

Actually, I am a beginner. This is the code which in the CodeIgniter User Guide. I tried this using Dreamweaver IDE.It works. But when I using Eclipse it is not working. I did all the things which are mentioned by you and by referring the Google.

 
Posted: 29 May 2012 06:20 AM   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2007-09-09
52 posts

if you have this line

$db['default']['autoinit'TRUE

in your database.php configuration

and in the autoload.php have:

$autoload['libraries'= array('database'); 

you do not need to use

$this->load->database();

 Signature 

array(“gamer”=>“sometimes”, “geek”=>“i try”, “webdeveloper”=>“everyday”, “mac”=>“wannabie”);