Skip to main content

Configuring Database Connections in CodeIgniter

How to configuring database connections in CodeIgniter, with additional information

Table of Contents

Introduction

CodeIgniter is a powerful PHP framework that provides a simple and elegant way to handle database interactions. In this tutorial, we will learn how to configure database connections in CodeIgniter and perform common database operations.

Setting up the Database Configuration

To configure the database connection in CodeIgniter, follow these steps:

Database Types

CodeIgniter supports various database types, including MySQL, PostgreSQL, SQLite, Oracle, and more. To specify the database type, set the 'dbdriver' parameter in the database.php configuration file accordingly.

Database Connection Parameters

Open the application/config/database.php file and modify the following parameters according to your database setup:

  • 'hostname': The hostname or IP address of your database server.
  • 'username': The username to connect to the database.
  • 'password': The password to connect to the database.
  • 'database': The name of the database you want to connect to.
  • 'dbdriver': The type of database driver to use (e.g., 'mysqli' or 'pdo').

You can also configure additional parameters such as port, character set, and more depending on your database requirements.

Connecting to the Database

To establish a database connection in CodeIgniter, follow these steps:

  1. Load the database library by adding the following line in your controller or model:

    $this->load->database();
  2. Once the library is loaded, you can access the database object using $this->db and perform various database operations.

Executing Queries

CodeIgniter provides a convenient query builder class that allows you to build and execute database queries easily. Here are examples of different types of queries:

Select Queries

To perform a select query and retrieve data from the database, you can use the following code:

$query = $this->db->get('users');
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
echo $row->username;
}
}

Insert Queries

To insert data into a table, you can use the following code:

$data = array(
'username' => 'john',
'email' => 'john@example.com',
'password' => 'secret'
);

$this->db->insert('users', $data);

Update Queries

To update data in a table, you can use the following code:

$data = array(
'email' => 'new_email@example.com',
'password' => 'new_secret'
);

$this->db->where('username', 'john');
$this->db->update('users', $data);

Delete Queries

To delete data from a table, you can use the following code:

$this->db->where('username', 'john');
$this->db->delete('users');

Closing the Database Connection

CodeIgniter automatically closes the database connection when the script finishes executing. However, if you want to manually close the connection, you can use the following code:

$this->db->close();

Conclusion

In this tutorial, we have learned how to configure and establish database connections in CodeIgniter. We have explored various types of queries such as select, insert, update, and delete. Now you can leverage the power of CodeIgniter to interact with databases efficiently.