Fatal error: Calling member function get () for non-object in C: \ wamp \ www \ ci \ application \ models \ site_model.php on line 6

Hello, I was just watching the first / Day 1 screencast on Nettuts "CodeIgniter from scracth". And I'm already running into an error that I don't understand. Here's a screenshot of http://i39.tinypic.com/14mtc0n.jpg

The code in my models \ site_model.php is the same as screencast

   models\site_model.php

   class Site_model extends CI_Model {  
   function getAll() {
    $q = $this->db->get('test');        
    if($q->num_rows() > 0) {
        foreach ($q->result() as $row) {
            $data[] = $row;
        }
    return $data;
    }
}

      

And controller controllers \ site.php

   class Site extends CI_Controller {
function index(){
    $this-> load-> model('site_model'); 
    $data['records'] = $this-> site_model-> getAll();
    $this-> load-> view('home', $data);

}   
 }

      

And here is my db info incase

 $db['default']['hostname'] = 'localhost';
 $db['default']['username'] = 'root';
 $db['default']['password'] = '';
 $db['default']['database'] = 'ci_series';
(rest is default below)

      

thank

+3


source to share


2 answers


First you need to load the database. Codeiginiter will not download it by default for you.

You can either add it to the /config/autoload.php

way how to do it

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

      

Or you can load it on demand whenever you want by calling



$this->load->database();

      

More details here

http://codeigniter.com/user_guide/database/connecting.html

+11


source


Add a constructor to you if you havenot:


class Site_model extends CI_Model {  

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

      



And: database autoload by modifying app /config/autoload.php if you don't have autoload

0


source







All Articles