cerberus478 Posted August 10, 2012 Share Posted August 10, 2012 Hi I'm using codeigniter and I'm busy doing a tutorial called codeigniter from scratch but I get an error saying A PHP Error was encountered Severity: Notice Message: Undefined property: Site::$db Filename: core/Model.php Line Number: 51 and Fatal error: Call to a member function get() on a non-object in C:\wamp\www\ci\application\models\site_model.php on line 7 I have done everything like the tutorial said here is my controller <?php class Site extends CI_Controller{ function index(){ $this->load->model('site_model'); $data['records'] = $this->site_model->getAll(); $this->load->view('home', $data); } } ?> This is my 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; } } } ?> This is my view <html> <head> <title> </title> </head> <body> <div id="container"> <p>My view has been loaded</p> <pre> <?php print_r($records); ?> </pre> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
MMDE Posted August 10, 2012 Share Posted August 10, 2012 <?php class Site extends CI_Controller{ function index(){ $this->load->model('site_model'); $data['records'] = $this->site_model->getAll(); $this->load->view('home', $data); } } ?> This is my 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; } } } ?> Of course the problem lies in this part of the code... more exactly here: $q = $this->db->get('test'); Property is usually what you call a variable in a class. You are using $this, which refers to the class it's in, and then you try to access the variable/property in it called $db. I can't see any such variable declared within the scope of the class, neither anywhere in your script... You also use this property as if it was a class, I suppose it's a mysqldb class of some sort, though I can't see anything to it. Maybe it's a part of codeigniter or something, I don't know, I've never used it, but I sure can't see it where you are referring to it to be. Quote Link to comment Share on other sites More sharing options...
cerberus478 Posted August 10, 2012 Author Share Posted August 10, 2012 the db in $this->db is suppose to get the database. It is part of codeigniter Quote Link to comment Share on other sites More sharing options...
cerberus478 Posted August 10, 2012 Author Share Posted August 10, 2012 Hi Thanks for your help I just realized that my database name was spelled differently then when I add it to the coding Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.