4

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

The code in my models\site_model.php is the same as the 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 the 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's 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 you

Matt
  • 2,439
  • 7
  • 29
  • 42
  • "Is it saying that 'test' is a bad parameter?" -- could you please read the error message once again? – zerkms Mar 09 '12 at 05:23
  • 1
    possible duplicate of [Codeigniter model error](http://stackoverflow.com/questions/8625703/codeigniter-model-error) – Wesley Murch Mar 09 '12 at 05:25
  • Thanks Madmartigan, that thread solved my problem, I needed to have "$this->load->database();" which the screencast did not do. Oh well, the screencast is about 2 years go. – Matt Mar 09 '12 at 05:50

2 Answers2

12

You need to load the database first. Codeiginiter won't load it by default for you.

You can either add it to /config/autoload.php like so

$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

JohnP
  • 49,507
  • 13
  • 108
  • 140
  • Got it, Yeah, it looks like it needed to have "$this->load->database();" as the first line after "function index(){" however, I don't how the screencast got pass it... but then again the video is about 2 years ago... – Matt Mar 09 '12 at 05:48
  • He might have just auto loaded it. That's the usual process for a DB driven app. – JohnP Mar 09 '12 at 17:15
  • Oh.. so does that mean he typed in a code I didn't see or was it his local host program/server that did this? He had MAMP loaded up, I had WAMP. I would definitely like to know what I did wrong. – Matt Mar 10 '12 at 17:36
0

Add constructor to you model, if you havenot:


class Site_model extends CI_Model {  

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

And: autoload database by changing application/config/autoload.php if you have not autoloaded

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162