Current Location: Home> Latest Articles> Complete Guide to Configuring and Using Database Connections in the CodeIgniter Framework

Complete Guide to Configuring and Using Database Connections in the CodeIgniter Framework

M66 2025-06-11

CodeIgniter Database Connection Setup Tutorial

CodeIgniter is a lightweight PHP framework known for its simplicity and performance, making it a popular choice for web application development. When working with CodeIgniter, database operations are essential. This guide walks you through the process of configuring a database connection and performing common operations using CodeIgniter.

Step 1: Configure Database Parameters

To begin using a database, you first need to set up your connection details. Open the application/config/database.php file in your project directory and fill in the database configuration settings, such as the database type, host, username, password, and database name. Here's an example configuration:

$db['default'] = array(
    'dsn'       => '',
    'hostname'  => 'localhost',
    'username'  => 'root',
    'password'  => 'root',
    'database'  => 'mydatabase',
    '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' => TRUE
);

Step 2: Load the Database Library

After configuring the database settings, load the database library in your controller or model using the following code:

$this->load->database();

This automatically initializes the database connection using the default settings specified in database.php.

Step 3: Perform Common Database Operations

CodeIgniter provides a variety of functions for interacting with the database. Below are some commonly used operations:

Executing a query:

$query = $this->db->query("SELECT * FROM tableName");

Inserting data:

$data = array(
    'columnName'  => 'value',
    'columnName2' => 'value2'
);
$this->db->insert('tableName', $data);

Updating data:

$data = array(
    'columnName'  => 'value',
    'columnName2' => 'value2'
);
$this->db->where('columnName', $value);
$this->db->update('tableName', $data);

Deleting data:

$this->db->where('columnName', $value);
$this->db->delete('tableName');

These operations are straightforward and can be adjusted based on the needs of your application.

Step 4: Handle Query Results

After running a query, CodeIgniter offers multiple methods to handle the results:

Get a single row:

$row = $query->row();

Get multiple rows:

$result = $query->result();

Get a specific column value:

$value = $row->columnName;

Get the number of rows returned:

$numRows = $query->num_rows();

These methods enable efficient data handling for various application needs.

Conclusion

Creating and using a database connection in CodeIgniter is a streamlined process. With proper configuration and loading of the database library, developers can easily perform CRUD operations. The framework’s intuitive and flexible interface makes it easy to manage data in web applications of all sizes.