How To Connect Database In CodeIgniter With Example

ci connection

First, you need to make sure that you have the correct configuration settings for your database. You can find these settings in the application/config/database.php file. In this file, you will need to specify your hostname, username, password, and database name. Once you have entered these settings, you will then be able to connect to your database.

For example:

$db[‘default’] = array(
‘dsn’ => ”,
‘hostname’ => ‘localhost’,
‘username’ => ‘root’,
‘password’ => ”,
‘database’ => ‘test_db’

Next, you will need to load the database driver. You can do this by adding the following line of code to your application/config/autoload.php file:

$autoload[‘libraries’] = array(‘database’);

Once the database driver has been loaded, you will then be able to use CodeIgniter’s Database class methods to interact with your database. For example, if you wanted to run a query on your database, you could use the following code:

$query = $this->db->query(“YOUR QUERY HERE”);

If you want to learn more about working with databases in CodeIgniter, we recommend reading through the official documentation:


Please enter your comment!
Please enter your name here