I need to read some data from database
my controller is
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));
}
}
and my model is :
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();
}
}
but it doesn’t work and echo following:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Pro_ajax::$db
Filename: core/Model.php
Line Number: 50
where is the problem,and how should I solve it?
with special thanks for your attention
I am 100% sure that you have not added database in your autoload.
please go to your config folder then open autoload.php and search for line having
$autoload[‘libraries’].
add this:
or you can do this:
Hope this helps.