Current Location: Home> Latest Articles> How to Use the Database Query Builder in the CodeIgniter Framework for Efficient Queries and Data Operations

How to Use the Database Query Builder in the CodeIgniter Framework for Efficient Queries and Data Operations

M66 2025-06-13

Introduction

CodeIgniter is a lightweight PHP framework widely used in web application development. It provides a powerful tool called the Database Query Builder (Query Builder), which helps developers simplify and optimize database operations. This article will guide you on how to use the Database Query Builder in the CodeIgniter framework for performing database queries, data insertions, updates, and more.

Initializing Database Configuration

Before using the Database Query Builder, you first need to configure the database connection. The configuration file is located in CodeIgniter's config/database.php file. In this file, you can set the database driver, host, username, password, and other information. Here is a sample configuration:

$this->db->initialize();
    

Querying Data

Performing queries using the Database Query Builder is straightforward. Below is a basic query example:

$this->db->select('name, email');
$query = $this->db->get('users');
$result = $query->result();
foreach ($result as $row) {
    echo $row->name;
    echo $row->email;
}
    

In the above example, we first use the select() method to choose the columns we want to retrieve. Then, the get() method is used to execute the query and get the result set. Finally, the result() method is used to convert the result into an array or object, and we loop through the results.

Adding Conditions and Sorting

In addition to basic queries, CodeIgniter's Database Query Builder offers a variety of methods to enhance query functionality:

  • Adding conditions:
$this->db->where('id >=', 100);
$this->db->where('name !=', 'John');
    
  • Sorting results:
$this->db->order_by('name', 'DESC');
    
  • Joining tables:
$this->db->join('orders', 'users.id = orders.user_id');
    

Inserting Data

The Database Query Builder also supports data insertion. Below is an example of inserting data:

$data = array(
    'name' => 'John',
    'email' => 'john@example.com',
    'phone' => '1234567890'
);
$this->db->insert('users', $data);
    

In this example, we first create an associative array containing the data to be inserted. Then, we use the insert() method to insert the data into the "users" table.

Updating Data

When updating data, you can use the where() method to specify the update conditions and the update() method to perform the update. Below is an example of updating data:

$data = array(
    'name' => 'John Doe',
    'email' => 'john.doe@example.com',
    'phone' => '9876543210'
);
$this->db->where('id', 1);
$this->db->update('users', $data);
    

In this example, we first create an associative array containing the data to be updated. Then, we use the where() method to specify the row to update, followed by the update() method to perform the update.

Conclusion

The Database Query Builder in CodeIgniter is a powerful and flexible tool that allows you to efficiently perform database queries. This article provided examples for initializing database configuration, querying data, inserting data, and updating data, which will help you better utilize this feature. By using the Database Query Builder, you can manage and operate databases more effectively while avoiding hard-coding and SQL injection risks.