I am new to codeigniter, and I have developed a code to carry out queries on the database. I load the database using$this->load->database();
and perform a query, but when I run the code, the browser gives me the following error message:
A PHP Error was encountered Severity: Notice Message: Undefined property: Tutorial::$load.
Fatal error: Call to a member function database() on a non-object
This is the code I am using:
class Tutorial extends CI_Controller {
public function tutorial() {
$this->load->database();
$query = $this->db->query('SELECT user,pass,email FROM tablex');
foreach ($query->result() as $row) {
echo $row->title;
echo $row->name;
}
I am sure the $db
variables in my database configuration file are properly set and I have even tried autoloading the database for all pages in the autoload.php config file; still having the same problem. Any ideas how to go about this?
Change
to
database is not a direct method. It is a library in codeigniter and you have to load it as a library.
You can also autoload
database
library inautoload.php
.UPDATE:
You are using the same name for your class and method. In PHP4, a method which has the same name as class name was treated as constructor, but if you are using codeigniter 2+, you have to use PHP5 constructor which is
You cannot give a method same name as class name in Codeigniter 2+. Change the method to anything else. You can name the method
index
if you want it to load by default.This should solve your problem.
CodeIgniter User Guide, Creating Libraries Section:
Hope this helps. You could also put the $CI in a constructor.
Your code would look something like this: