Starlays Starlays - 7 months ago 96
PHP Question

Codeigniter 3 Fatal error: Call to a member function database() on null

I think that can't manage to connect to the database and I don't know why.

I have looked over stackoverflow and found this questions:
here and it is not helping me in solving my problem.

I have read the documentation from Codeigniter 3: here and used the option Manually Connecting.

My class from my application controller looks like so:

class home extends CI_Controller {

* Class constructor
* Load database lib
public function __construct()


* Index Page for this controller.
* Maps to the following URL
* - or -
public function index()
$query = $this->db->get('users');

foreach ($query->result() as $row)
var_dump($row->fullName); //testing purpose

//$this->load->view('home', $data);


The database config from my application looks like so:

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'user',
'password' => 'password',
'database' => 'tasks',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => FALSE

And when I access http://localhost/home.php/welcome

I get this error:

Fatal error: Call to a member function database() on null in
\www\task\application\controllers\home.php on line 12

I tried var_dump($this->load) and it is a null and from here my assumption that it can't establish a connection to the database.


Since you are extending the CI_Controller class and have opted to overload the __construct method, you need to just call the parent construct before you can begin taking advantage of CI's core functions.

class home extends CI_Controller
    public function __construct()
        // $this->load does not exist until after you call this
        parent::__construct(); // Construct CI's core so that you can use it


See for further details.