![](/img/trans.png)
[英]Fatal error: Call to a member function query() on a non-object in (CodeIgniter)
[英]Fatal error: Call to a member function result() on a non-object in Codeigniter
致命錯誤:在第111行的D:\\ wamp \\ www \\ ocss \\ application \\ core \\ My_Model.php中的非對象上調用成員函數result()
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
}
}
背后的原因是什么?
編輯
這是我的材料模型,它具有表名稱和所有表字段
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;
}
這是我的MY_Model,它具有表名和get方法以獲取所有結果
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;
}
最后,我通過簡單地調用mysql查詢而不是存儲過程解決了我的問題
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("My Query To Database")->result_array();
$material = $this->Material_Model->get(); // now when i call it here there is no error
}
}
但是我很困惑我在調用存儲過程而不是查詢時會出錯
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";
}
}
}
在模型中
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.