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
https://forums.phpfreaks.com/topic/266904-undefined-property/
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
https://forums.phpfreaks.com/topic/266904-undefined-property/#findComment-1368340
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.