Jump to content

undefined property


cerberus478

Recommended Posts

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>		

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.