Undefined property $db in Codeigniter

Undefined property $db in Codeigniter

Are you facing any notice saying Undefined property: Home::$db or something like this in your Codeigniter application? The reason behind the error Undefined property $db in Codeigniter arises because you didn’t load database library in your controller/method to use any function of it.

This error also produces another Fatal error: Call to a member function on a non-object in your code. 

Fix to Undefined property $db

To fix this error you should first include database library form Codeigniter in your controller/model or using Auto-load feature. Since database library is used at multiple places in your application so it would be better to load database library automatically when your system runs. So open your application >  config > autoload.php file and replace

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

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

Or alternatively you can call the following line in your controller construct/model or function if you are using database at only one or two places:


You Might Interested In

  • Mauro Lopreste

    Thank you so much! 🙂

  • Asep Saepuloh

    Thank’s very much