douren5898 2015-12-12 19:48 采纳率: 100%
浏览 83

致命错误:在Codeigniter中的非对象上调用成员函数result()

Fatal error: Call to a member function result() on a non-object in D:\wamp\www\ocss\application\core\My_Model.php on line 111

class Report extends MY_Controller{
    public function item_ladger()
    {
        $material = $this->Material_Model->get(); // when i call it here it works fine
        $inventory = $this->db->query("CALL inventory($id)")->result_array();
        $material = $this->Material_Model->get(); // when i call it here it Generate Fatal error: Call to a member function result() on a non-object in
    }
}

what's the reason behind?

EDIT

this is my material model it has table name and all table fields

class Material_Model extends MY_Model
{
    const DB_TABLE = 'material';
    const DB_TABLE_PK = 'material_id';

    public $material_id;
    public $material_name;
    public $size;
    public $rate;
}

this is my MY_Model it has table name and get method to get all result

class MY_Model extends CI_Model {
    const DB_TABLE = 'abstract';
    const DB_TABLE_PK = 'abstract';

    public function get($limit = 500, $offset = 0,$desc=true) {
    if ($limit) {
        if ($desc)
            $query = $this->db->order_by($this::DB_TABLE_PK, 'DESC')->get($this::DB_TABLE, $limit, $offset);
        else
            $query = $this->db->get($this::DB_TABLE, $limit, $offset);
    }
    else {
        $query = $this->db->get($this::DB_TABLE);
    }

    $ret_val = array();
    $class = get_class($this);

    foreach ($query->result() as $row) {
        $model = new $class;
        $model->populate($row);

        $ret_val[$row->{$this::DB_TABLE_PK}] = $model;
    }
    return $ret_val;
}
  • 写回答

2条回答 默认 最新

  • dongxue7306 2015-12-12 19:58
    关注
    class Report extends MY_Controller{
        public function item_ladger()
        {
            $this->load->model("Material_Model"); # loding Model
            $inventory = $this->Material_Model->get_data(); # caling Model to do my code
    
            if ($inventory['cup']) { # retrivig data from retund array
                echo "I never ate Cup Cakes";
            }
    
        }
    }
    

    In Model

    public function get_data()
    {
        $query = $this->db->get('cake'); # get data from table
        $result = $query->result_array(); # re-Assign as objective array
        return $result; # return data
    }
    

    Codeigniter SELECT

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?