Codeigniter $this->db->get(), how do I return values for a specific row?

Codeigniter $this->db->get(), how do I return values for a specific row?

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

Stefan Bogdanescu

Founder & Senior Architect

Extracting Scalar Values from CodeIgniter Query Results

In CodeIgniter, the Active Record class provides a flexible interface for database interactions, but it often wraps results in objects rather than returning raw scalar values. When you execute a query like $this->db->where('id', '3')->get('my_users_table'), the result is stored in $q. To retrieve the age column for that specific user, you must unpack this object.

The most common approach is to use the row() method or first() method if you are using CodeIgniter 4. In older versions (CodeIgniter 3), the standard result() method returns an array of objects representing every row in the result set. Since your query filters by a unique ID, you will get back exactly one object inside that array. Accessing the property directly from this single object is possible, but using row() is generally cleaner and more explicit when dealing with a single record.

Here is how you can extract the age:

$q = $this->db->where('id', '3')->get('my_users_table');

// Returns an array containing one object
$results = $q->result(); 

// Access the first (and only) row from the array
$user = $results[0];

// Access the specific property
$age = $user->age;

While this works, it requires handling the array index manually. A more direct method is row(), which returns the single result object directly without wrapping it in an array:

$q = $this->db->where('id', '3')->get('my_users_table');
$user = $q->row();

// Now you can access the property directly
$age = $user->age;

Alternatively, CodeIgniter offers a convenience method called result_object(). This returns a single object if one row is found. However, this method will return null if no rows are found, which can lead to "Undefined property" errors in PHP if you attempt to access $user->age without checking for existence first. Using row() is often safer because it returns false instead of an object when no data exists, allowing you to write a simple conditional check:

if ($q->row()) {
    $age = $q->row()->age;
} else {
    // Handle the case where the user was not found
    $age = null; 
}

Handling Null Values Safely

A critical pitfall in this scenario is assuming that a column exists or contains a value. Even if you filter by ID, the age column might be NULL in the database. Attempting to echo a NULL property works fine in PHP, but it can cause issues if you try to perform arithmetic operations on it without validation.

If you prefer a more modern approach, consider using the query builder with specific field selection. By selecting only the fields you need, you reduce memory overhead and ensure that the result object contains exactly what you expect:

$q = $this->db->select('age')
              ->where('id', '3')
              ->get('my_users_table');

$user = $q->row();
$age = $user->age; // Will be null if not found or if the value is null in DB

This approach also prevents accidental exposure of sensitive data if your table contains columns you do not need. It forces you to explicitly request only the necessary information, aligning with the principle of least privilege in data access.

Migration Considerations

If you are currently working with CodeIgniter 3 and planning a migration to CodeIgniter 4, note that the syntax for retrieving results changes slightly. In CI4, result() is often replaced by first(), last(), or simply iterating over the result collection as an iterable. The underlying logic remains similar: you are still dealing with an object representation of your database rows.

For teams looking to streamline their workflow further, exploring frameworks that emphasize type safety and stricter error handling might be beneficial. Resources like https://laravelcompany.com offer insights into modern PHP practices that could complement or replace legacy Active Record patterns in the future. Understanding how to extract scalar values efficiently is a foundational skill that translates well across these different ecosystems.

Our network