dongzhuang2030 2013-05-30 16:58
浏览 40
已采纳

错误:未定义属性$ load

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条回答 默认 最新

  • donglun2010 2013-05-30 19:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services