How to return only a specified column value from the first row of a CodeIgniter query

How to return only a specified column value from the first row of a CodeIgniter query

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Accessing Single Values from CodeIgniter Queries Efficiently

In the CodeIgniter framework, handling database results often follows a specific pattern that can feel slightly verbose when you only need a single value. Consider a scenario where your application logic expects exactly one record based on a unique identifier, such as fetching the campaign_id for a specific user session. The standard approach involves executing the query, converting the result set into an array of objects, and then accessing the first element:

$query = $this->db->get();
$ret = $query->result();
return $ret[0]->campaign_id;

While this method is functional, it requires three steps to retrieve what should logically be a single piece of data. The immediate question usually arises: is there a dedicated function to return the first row directly? The answer lies in the row() method provided by CodeIgniter's Query Builder. By calling $query->row(), you bypass the need to load the entire result array. This returns a single object representing the first row of the result set. If your query is guaranteed to return exactly one row, accessing properties on this object becomes significantly more concise:

$query = $this->db->get('campaigns', 'campaign_id', ['conditions']);
return $query->row()->campaign_id;

This approach reduces boilerplate code and makes the intent of the function clearer. You are explicitly stating, "I expect one row, and I want this property," rather than iterating through an array to find the first item.

However, the ideal scenario described in your inquiry—accessing properties directly on the query object like $query->campaign_id—is not natively supported in CodeIgniter's core database class. The Query Builder object manages the SQL construction and execution; it does not automatically map columns to public properties upon completion of a get() call. To achieve this level of direct access, you would need to manually construct a custom property or utilize a specific configuration that is not part of the default framework setup. Attempting to assign values directly to the query object without proper encapsulation can lead to scope issues or memory leaks if not handled carefully within the controller or model lifecycle.

There are distinct trade-offs between using row() and result(). The result() method returns an array of objects, which is useful when you anticipate multiple rows or need to iterate over the dataset. Conversely, row() is optimized for single-record retrieval. If your query logic ensures a single row exists (e.g., querying by a unique primary key), using row() prevents unnecessary memory overhead associated with loading an array wrapper. It also simplifies debugging, as you can inspect the returned object directly without traversing an index.

A critical pitfall to watch for is handling cases where the query might return zero rows. If the record does not exist, $query->row() will return null. Accessing a property on this null value (e.g., $query->row()->campaign_id) will trigger a fatal error or a notice depending on the PHP error reporting level. Robust code must account for this possibility:

$row = $this->db->get_where('campaigns', ['user_id' => $id])->row();

if ($row) {
    return $row->campaign_id;
}
return null; // Or handle the "not found" state appropriately

While some developers might look for external libraries or custom hooks to make the query object act like a simple array of properties, sticking to the native row() method is generally preferred. It leverages the framework's built-in abstraction layer, ensuring compatibility across different CodeIgniter versions and reducing the risk of introducing security vulnerabilities through custom database wrappers.

If you are considering moving away from CodeIgniter or exploring more modern PHP frameworks for future projects, platforms like LaravelCompany often highlight how newer architectures handle eager loading and model attributes differently, sometimes offering more direct property accessors out of the box. However, within the current CodeIgniter ecosystem, respecting the distinction between row() and result(), and handling null returns explicitly, remains the most reliable pattern for accessing single column values from database queries.

Our network