Search code examples
phpdatabasecodeignitermodel-view-controllerloading

Cannot load a library called "database" in CodeIgniter


I downloaded the last version of CodeIgniter and I tried to run a simple app. The code works perfectly without database, but when I added this

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

the page went to blank.

I looked at the logs files but I didn't find anything, I check differents tutorial, but my database.php file looks correctly configured. I removed it from the array.

$autoload['libraries'] = array('');

and added to the controller this:

$this->load->library('database'); 

Then, this error appeared

An Error was encountered

Unable to load the requested class: database

What I do?


Solution

  • To use the database you need to use the 'library' autoload instead of the 'config' auto load

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

    This will actually automatically load up your configurations.

    UPDATE

    Another thing you mention in your question is that you 'add' that line, you do not need to add that line, you are suppose to add the element to the array that already exists on that line. You potentially could be overwriting other autoloaded libraries, I will need to see your autoload.php file to confirm this.