簡體   English   中英

Codeigniter-致命錯誤:在非對象上調用成員函數get_news()

[英]Codeigniter- Fatal error: Call to a member function get_news() on a non-object

當我嘗試運行一個簡單的函數時,它向我顯示:

遇到PHP錯誤

嚴重程度:注意

消息:未定義的屬性:Site :: $ model_users

文件名:controllers / site.php

行號:14

致命錯誤:在第14行的/hermes/bosoraweb130/b418/ipg.blazewarcom/ci/application/controllers/site.php中的非對象上調用成員函數get_news()

功能:

位於models文件夾中的model_users.php:

public function get_news()
{
    $query = $this->db->query("SELECT * FROM tblnews ORDER BY newId DESC LIMIT 3");
    return $query;
}

site.php位於controllers文件夾中:

public function home_news() {
        $query = $this->model_users->get_news(); //This line causes the problem
        ...
        ...
}

您是否只想在$this類上調用get_news()方法?

嘗試

class Site {
   public function home_news() {
        $class_name = new Class_Name();  //change Class_Name() to the class that has the method get_news.
        $query = $class_name->get_news(); //This line caused the problem
        ...
        ...
   }
}

希望這可以幫助。

這應該是這樣的:

模型:

class Model_users extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function get_news()
    {
        $query = $this->db->query("SELECT * FROM tblnews ORDER BY newId DESC LIMIT 3");
        return $query;
    }
}


控制器:

class Site extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('model_users');
    }

    public function home_news() 
    {
        $query = $this->model_users->get_news(); //This line causes the problem
        ...
        ...
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM