Error: undefined property $load

2019-08-07 16:37发布

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?

2条回答
做自己的国王
2楼-- · 2019-08-07 17:28

Change

$this->load->database();

to

$this->load->library('database');

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 in autoload.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

function __construct()
{
    parent::__construct();
    /*Additional code which you want to run automatically in every function call */
}

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.

查看更多
冷血范
3楼-- · 2019-08-07 17:31

CodeIgniter User Guide, Creating Libraries Section:

To access CodeIgniter's native resources within your library use the get_instance() function. This function returns the CodeIgniter super object. Normally from within your controller functions you will call any of the available CodeIgniter functions using the $this construct.

$this, however, only works directly within your controllers, your models, or your views. If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:

First, assign the CodeIgniter object to a variable:

$CI =& get_instance();

Once you've assigned the object to a variable, you'll use that variable instead of $this:

$CI =& get_instance();

$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
etc.

Hope this helps. You could also put the $CI in a constructor.

Your code would look something like this:

class Tutorial
{
    public $CI;

    /**
     * Constructor.
     */
    public function __construct()
    {
        if (!isset($this->CI))
        {
            $this->CI =& get_instance();
        }
        $this->CI->load->database();
    }
}
查看更多
登录 后发表回答