我正在为基于CodeIgniter的网站加载模型,但它一直出错:

"Fatal error: Call to a member function on a non-object in /nfs/c02/h05/mnt/30796/domains/planetchustar.com/html/arguit/system/application/controllers/home.php on line 8"

以下是其引用的代码部分:

function index()
{
        $this->load->model('posts');    //error here
        $result = $this->Posts->get_all_topics();
}

该模型被称为“帖子”。它的文件名是“posts.php”。

编辑: 我发现了我的一个问题,那就是在我尝试使用它的函数之前我没有加载到数据库,所以我修复了它,但现在却说:

A Database Error Occurred
Unable to connect to your database server using the provided settings.

但我确定我在database.php文件中保存的连接信息是准确的(来自phpmyadin网站)。

有帮助吗?

解决方案

在这种情况下,codeigniter用户指南有点难以理解。只有文件名应为小写,“加载”和调用($ this-> Posts-> etc())应与模型文件中的声明匹配。

这绝对是一个区分大小写的问题。

其他提示

'posts'是否在子目录中?如果是这样,那么您需要在加载期间引用子目录。

如果不是这样,那么这里是一个可能有帮助的帖子。

我同意@arthur。确保您没有尝试将控制器嵌入另一个控制器中。

这是区分大小写错误....

像这样使用它:

function index()
{
        $this->load->model('posts');    //error here
        $result = $this->posts->get_all_topics();//<-- Notice "posts" and not "Posts"
}

这意味着$ this-&gt; load不是对象引用(很可能是null)。为什么一旦你知道这一点就很容易确定。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top