How to connect to MySQL database in CodeIgniter?

Yesterday someone asked me to make a tutorial about Connecting to Database in CodeIgniter Framework. In order to make connection to database in Ci , we need to do only few configuration changes in your database.php config file which is located at application / config / database.php.

After making changes to database.php config file, we can connect to the database in two ways.
First one is Automatic loading, to do this just go to autoload.php file which is located at application/ config / database.php file. Then add ‘database’ to $autoload[‘libraries’] = array() array.

Another way of loading is manual loading with $this->load->database() method. place this line to load database in your controller contractor or with in the method(s).

Sometimes you may get fallowing error i got this error message with net4india shared hosting environment

In order to fix this issue , i simple changed config values of pconnect to false.

That’s it for database connecting and loading in CI, if you have any issues , let me know via comments.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.