You make a very common mistake. When you call $this->load->database(); from controller or model , this works because controllers and models are children of CI_Controller and CI_Model respectively. But when you call them from a library that is not a child of any CI base class, you cannot load database () or anything else using the $this-> key. you should use the help &get_instance(); to load the codeigniter instance and use this instance instead of $this . Which suggests the following code:
$INST=&get_instance();//Store instance in a variable. $INST->load->database();//If autoload not used. $INST->db->get('people');//or Your desired database operation.
It is better to leave a field variable for storing the link to $INST , since you may need to access it in various functions.
The following code would be more appropriate:
class MyLib{ var $INST; public function __construct() { $INST=&get_instance();//Store instance in a variable. $INST->load->database();//If autoload not used. } function getPeople(){ $query = $INST->db->get('people'); return $query->result(); } }
source share