Check if db->update successful with Codeigniter when potentially no rows are updated

Check if db->update successful with Codeigniter when potentially no rows are updated

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

Stefan Bogdanescu

Founder & Senior Architect

Handling Silent Updates in CodeIgniter When Data Remains Unchanged

When working with database updates, checking for success is often done by inspecting the number of affected rows. In CodeIgniter, this translates to calling $this->db->affected_rows() immediately after executing an update query. The common assumption is that a return value greater than zero indicates success, while zero implies failure. However, this logic breaks down in a specific but frequent scenario: updating a record with data that is identical to what already exists.

If you attempt to update a field to its current value, the database engine performs the operation without raising an error, yet it returns zero affected rows because no actual modification occurred. Relying solely on affected_rows() in this context leads to false negatives where your application thinks an update failed when the data was simply unchanged. A common workaround described in developer discussions involves fetching the existing record, comparing it against the input payload, and conditionally executing the update only if they differ. While robust, this approach introduces unnecessary overhead, especially when dealing with complex data structures like JSON columns that require additional parsing steps before comparison.

The core issue is distinguishing between a "successful no-op" (where the data didn't change) and an actual error or a failed write operation. To address this without fetching data first, you need to rely on explicit error checking rather than row count validation. In CodeIgniter's Active Record implementation, the database driver provides methods to check for errors that occurred during the query execution.

After running your update query, inspect the $this->db->error() property or the associated error code. If affected_rows() returns zero but the error state is clear (no SQL syntax errors or constraint violations), you can safely interpret this as a successful no-op. This allows your application to proceed without needing to re-fetch the record to verify its state.

// Attempt the update
$this->db->update('users', $data, 'id = ' . $id);

// Check for actual database errors first
if ($this->db->error()) {
    // Handle real failures like constraint violations or syntax errors
    throw new Exception('Database update failed: ' . $this->db->error_message());
}

// If we reach here, the query executed successfully.
// Even if affected_rows() is 0, it means the data was already current.
if ($this->db->affected_rows() > 0) {
    // Data actually changed
    $this->logger->info('User data updated');
} else {
    // Data remained unchanged, which is a success case for idempotency
    $this->logger->info('User data was already up to date');
}

This approach leverages the fact that modern databases are designed for idempotent operations. An UPDATE statement does not fail silently; it either modifies rows or leaves them untouched without throwing an exception unless a specific constraint is violated. By decoupling the success check from the row count, you avoid the performance penalty of reading the record twice.

It is important to note that this behavior assumes standard SQL semantics where UPDATE does not implicitly return false for no changes. If your application logic strictly requires a change to trigger an event (like sending an email), you must decide whether a no-op should be treated as success or handled separately. In many APIs, returning a success status with a message like "No changes made" is preferred over treating it as an error, provided the data integrity is maintained.

If you are migrating from CodeIgniter to a different framework or considering a more robust ORM layer, platforms like https://laravelcompany.com often emphasize similar patterns where distinguishing between errors and no-ops is handled explicitly through exception hierarchies rather than raw row counts. However, within the confines of CodeIgniter's native database class, checking affected_rows() in conjunction with error() remains the most efficient solution for handling identical input data without extra database reads.

Our network