我需要從數據庫中讀取一些數據 我控制器CodeIgniter的模型不工作
class Pro_ajax extends CI_Controller{
public function _construct() { parent::_construct();
$this->load->model('ajax_model');
}
public function _remap($method,$param = array())
{
if ($method == 'user_search')
{
$this->user_search($param[0]);
}
}
private function user_search($text = '')
{
$result = $this->ajax_model->ajax_user_search($text);
$count = count($result);
$data = array();
for ($i = 0;$i < $count AND $i < 5;$i++)
{
$data[$i][0] = $result->id;
$data[$i][1] = $result->firstname.' '.$result->lastname;
}
echo (json_encode($data));
}
}
和我的模型是:
class Ajax_model extends CI_Model{
public function __construct() {
parent::__construct();
}
public function ajax_user_search($text = '')
{
$this->db->flush_cache();
$this->db->limit(5);
$this->db->or_like('firstname',$text);
$this->db->or_like('lastname',$text);
$this->db->or_like('alias',$text);
$this->db->or_like('username',$text);
$query = $this->db->get('user_store_table');
var_dump($this->db->last_query());
return $query->result();
}
}
,但它不工作和回聲以下:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Pro_ajax::$db
Filename: core/Model.php
Line Number: 50
問題在哪裏,我該如何解決? 特別感謝您的關注
你有沒有自動加載你的數據庫?在自動加載文件在配置? – AlphaMale
沒有,CI_Model不會自行做呢?我伸出從中 –
您正在使用哪個版本的CodeIgniter我的模型?你的代碼有點混亂。你可以請發佈完整的代碼,因爲我無法在任何地方看到Pro_ajax。 – AlphaMale