Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter : Cant load database in my library.

When I try to call

$this->load->database();

yields the following error `"Call to a member function database() on a non-object"
Autoloading the database doesnt help too...
when I try to autoload it.
all calls to database like

$this->db->get('people');

it says get method is undefined...
I have no clue what and where to start..
\ anyone ?

like image 639
Kevin Florenz Daus Avatar asked Dec 11 '22 16:12

Kevin Florenz Daus


2 Answers

Go to autoload.php in application/config/autoload.php and add this

$autoload['libraries'] = array('database'); // add database in array

Make sure your connection settings are fine in application/config/database.php

Than in the library do it like this

Class MyLib
{
    function getPeople(){
        $CI =   &get_instance();
        $query  =   $CI->db->get('people');
        return $query->result();
    }
}
like image 91
Muhammad Raheel Avatar answered Jan 02 '23 18:01

Muhammad Raheel


Use extends CI_Model if not working try extends Model

class User_model extends CI_Model { 

     public function __construct() 
     {
           parent::__construct(); 
           $this->load->database();
     }
}
like image 22
asvignesh Avatar answered Jan 02 '23 17:01

asvignesh