错误:未定义的属性$ load [英] Error: undefined property $load

查看:134
本文介绍了错误:未定义的属性$ load的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Codeigniter的新手,我已经开发了一个代码来对数据库进行查询.我使用$this->load->database();加载数据库并执行查询,但是当我运行代码时,浏览器会显示以下错误消息:

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;
        }

我确定数据库配置文件中的$db变量已正确设置,甚至尝试为autoload.php配置文件中的所有页面自动加载数据库;仍然有同样的问题.有任何想法如何解决这个问题吗?

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?

推荐答案

更改

$this->load->database();

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

数据库不是直接方法.它是codeigniter中的一个库,您必须将其作为库加载.

database is not a direct method. It is a library in codeigniter and you have to load it as a library.

您还可以在autoload.php中自动加载database库.

You can also autoload database library in autoload.php.

更新:

您为类和方法使用了相同的名称.在PHP4中,与类名同名的方法被视为构造函数,但是如果您使用的是Codeigniter 2+,则必须使用PHP5构造函数,

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 */
}

在Codeigniter 2+中,您不能为方法提供与类名称相同的名称.将方法更改为其他任何方法.如果要默认加载方法,可以将方法命名为index.

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.

这篇关于错误:未定义的属性$ load的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆