3

When using Codeigniter to load the database, I am getting the following error message in a simple file where I load the database:

An Error Was Encountered

Unable to load the requested class: database

The controller for this code is called db.php, and the code for it is as follows:

<?php

class Db extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('database');

    }

    public function index() {}

}

I do not have the database loaded in to autoload.php. I also know that I have the correct database information in my config.php file. My CodeIgniter application is in a subdomain if that might cause any problems.

Any ideas on what could be causing the problem? Thanks.

3
  • 4
    Why not just $this->load->database();? Or am I misunderstanding what you're trying to do? Commented Aug 4, 2013 at 17:05
  • will that perform the same as if I put $autoload['libraries'] = array('database'); into my autoload.php file? Commented Aug 4, 2013 at 17:09
  • this is a good example: $autoload['libraries'] = array('session','database','email','form_validation','cart','encrypt'); Commented May 15, 2022 at 13:34

2 Answers 2

12

Use $this->load->database();

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it worked but I don't understand why $this->load->library('database'); it's not working. It's the default way to load any library in ci.
0

You don't have to load the library as it is loaded automatically as soon as you add 'database' to library array in application/config/autoload.php, use $this->load->database(); and if u want to connect manually by a group name or as a data source name(dsn) please look into [https://www.codeigniter.com/userguide2/database/connecting.html][1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.