SELECT MAX column value relating to the MAX column value of another table with CodeIgniter's query builder

SELECT MAX column value relating to the MAX column value of another table with CodeIgniter's query builder

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

Stefan Bogdanescu

Founder & Senior Architect

SELECT MAX column value relating to the MAX column value of another table with CodeIgniter's query builder

The error message Object of class CI_DB_mysql_result could not be converted to string is a classic symptom of a type mismatch in your logic, not necessarily a failure of the database engine itself. It happens when you attempt to pass an object representing a result set directly into a method that expects a raw value, like a column name or an integer.

In your specific case, the issue lies in how you are handling the result from the first query before passing it to the second. When you execute $this->db->get('rapporten'), CodeIgniter returns a CI_DB_mysql_result object. You cannot use this entire object as the value for the where() clause in your subsequent query. The database needs the specific ID value (e.g., 42), not the object wrapper that holds it.

Extracting the Primary Key

To fix the immediate crash, you need to extract the scalar value from the result object before using it in the next step. In CodeIgniter 2.x, which your function signature suggests you are using, the result object provides a row() method. Calling this without arguments returns an associative array containing all columns selected by the previous query.

However, since your first query only calls select_max('id'), the resulting row will contain a single key named id with the maximum value. You can access it like so:

$result = $this->db->get('rapporten');
$max_id = $result->row()->id; // Extracts the integer ID

Once you have $max_id, you can safely pass it to the second query's where() clause. This transforms your workflow from chaining objects that fail type checks into a standard data flow where raw values move between steps.

Handling the Second Query and Data Types

After extracting the ID, your logic proceeds to select the periode_nummer from the statistieken_onderhoud table. It is crucial to remember that periode_nummer likely represents a numeric counter or an integer timestamp. When you retrieve it using $query->row()->periode_nummer, ensure you are handling potential NULL values, as foreign key relationships can sometimes be missing entries.

If the second query returns no rows, attempting to add 1 to NULL in PHP results in 1. While mathematically valid, this might not be your intended business logic if the record is missing entirely. You should verify that a row exists before performing arithmetic operations on it.

$query = $this->db->get('statistieken_onderhoud', [], 1); // Limit to 1 for performance
if ($query->num_rows() > 0) {
    $periode_nummer = $query->row()->periode_nummer;
    $data = $periode_nummer + 1;
} else {
    // Handle the case where no record exists, perhaps returning a default or error
    $data = null; 
}

Performance Considerations

While extracting the row is necessary for the logic to hold together, be mindful of performance if your tables grow large. Your current approach performs two separate queries: one to find the max ID and another to fetch the periode number.

If you are running this in a high-traffic environment, consider whether you can optimize this into a single query or ensure that indexes exist on rapporten.bedrijf_id and statistieken_onderhoud.rapporten_id. Without proper indexing, these lookups will degrade as your dataset expands. Furthermore, using select_max is generally efficient, but if the database engine does not have an index on id, it might perform a full table scan.

Conclusion

The root cause of your error was treating the result set object as a raw value. By explicitly extracting the ID from the first query's result row, you bridge the gap between the two tables correctly. Always ensure that values passed to database constraints or arithmetic operations are primitive types (strings or integers) rather than framework-specific objects. This pattern prevents type conversion errors and ensures your data logic remains robust across different database states.

Our network