Counting the number of results returned by a database query in Codeigniter

Counting the number of results returned by a database query in Codeigniter

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

Stefan Bogdanescu

Founder & Senior Architect

Why count() Always Returns 1 in CodeIgniter Query Results

When handling search logic or list views, you often need to know immediately if a database query returned any data. In standard PHP development, checking the length of an array feels intuitive: you simply wrap your result set in count(). However, users working within the CodeIgniter framework frequently encounter a specific quirk where this approach yields unexpected results. A developer might report that their conditional logic fails because if (count($result)) evaluates to true even when the database query returned zero rows.

The root of this issue lies in how CodeIgniter's Active Record class structures data. When you invoke a model method like $this->search_model->do_search('value'), the framework does not return a raw array or an object directly. Instead, it returns an instance of stdClass. This container object holds the actual query results within a property named result.

Consequently, passing the variable returned by your model directly to the count() function is problematic. If you execute count($result), you are technically counting the properties of that stdClass object, not the rows inside it. Since the framework initializes this container object regardless of whether data was fetched, the object itself always exists. This structural quirk often leads developers to mistakenly believe their query logic is flawed when, in reality, they are counting the wrong variable.

To fix this, you must access the underlying property where the actual data resides. The correct pattern involves dereferencing the result property before applying the length check. Instead of passing the container object directly, you should pass $result->result. This ensures that count() operates on the array of rows returned by the database engine.

$data['result'] = $this->search_model->do_search(set_value('name'));
$this->load->view('search_view', $data);

// In your view or controller logic:
if (count($data['result']->result) > 0) {
    // Display results table
} else {
    // Handle empty state
}

There is a second, often overlooked pitfall related to the execution method itself. CodeIgniter distinguishes between "safe" queries and "unsafe" queries based on how the result set is structured. When you use SELECT * or multiple columns in your SQL statement, the database returns a standard array of objects. This behaves as expected with count(). However, if your query uses SELECT COUNT(*) or aggregates data into a single column, the framework wraps that scalar value into an array containing a single object. In these specific aggregation scenarios, $result->result might be an array containing one element rather than zero elements if no match is found, depending on the specific version of CodeIgniter and how the query was constructed.

While checking the count after fetching all rows is straightforward, it comes with performance implications for large datasets. Fetching thousands of rows just to check if the list is empty is inefficient. A more robust architectural approach involves separating the existence check from the data retrieval. You can execute a SELECT COUNT(*) query first to verify if any records exist before attempting to fetch the full dataset. This pattern ensures that you only load heavy rows into memory when necessary, optimizing both CPU usage and database load times.

For developers migrating from CodeIgniter to modern stacks like Laravel, this distinction between container objects and raw data is less pronounced but still relevant. Modern frameworks often return collections or query builders that handle these checks differently. If you are building a reusable solution for enterprise applications, understanding these framework-specific behaviors prevents subtle bugs in search interfaces and dashboard widgets.

Our network