$this->db->last_query() returns NULL in Codeigniter 4. How to get query SQL?

$this->db->last_query() returns NULL in Codeigniter 4. How to get query SQL?

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

Stefan Bogdanescu

Founder & Senior Architect

Debugging the Last Query in CodeIgniter 4

In CodeIgniter 3, retrieving the raw SQL string of the most recent database operation was a straightforward task. Developers could simply call $this->db->last_query() to inspect what the framework had just sent to the server. This pattern persists in many legacy applications and is often the first instinct when debugging complex joins or aggregations in CodeIgniter 4. However, attempting to replicate this exact behavior with $this->db->last_query() in a modern CI4 application will yield NULL.

The reason for this discrepancy lies in how CodeIgniter 4 handles database bindings and query caching internally. Unlike the older version where the raw SQL string was stored as a persistent property on the database connection object, the Query Builder class in CI4 does not expose the final compiled SQL string via a simple getter method like last_query(). The underlying Query object maintains internal state regarding bindings and parameters, but it does not automatically retain the concatenated string representation of the query after execution unless explicitly requested.

When you run code like $cityModel->select('...')->join('...')->result(), the framework executes the query immediately within the method scope. By the time your controller returns to inspect $cityModel->db->getLastQuery(), the internal state required to reconstruct that specific SQL string has already been cleared or is inaccessible through the public API of the database connection class. Even casting the result to a string via var_export((string)$cityModel->db->getLastQuery()) will return an empty string because the method returns NULL by default in this context.

To debug your specific model logic involving multiple joins between cities, provinces, and counties, you must intercept the query execution before it runs or utilize a different debugging approach entirely. If you need to inspect the SQL for immediate validation or logging within the controller, you cannot rely on post-execution retrieval from the Database connection object.

Instead, consider using the built-in debugbar integration if your environment supports it. CodeIgniter 4 integrates seamlessly with DebugKit or Laravel-style debugging extensions that can log every query execution, including the full SQL string and binding parameters. This approach is generally preferred over manually capturing queries, as it provides a historical record of performance bottlenecks and syntax errors without cluttering your controller logic.

Alternatively, you can modify your CityModel to accept an option that forces the output of the raw query string during execution. However, this requires extending the base model or using a custom method since the core Query object does not have a public method to return the final compiled SQL after the run() method has completed.

For instance, if you are working in an environment where debugging is critical, enabling the debug toolbar in your .env file and checking the "Database" tab will display the exact SQL generated by your complex joins:

# .env configuration
CI_DEBUG = 1
CI_ENVIRONMENT = local

Once enabled, any call to $cityModel->result() will trigger a log entry that you can inspect immediately after execution. This is often safer than trying to reverse-engineer the query from the connection object, as it guarantees you are seeing the exact statement sent to the database server, including all applied bindings.

If you are migrating logic from CodeIgniter 3 and find yourself frequently needing to log SQL for analysis, this architectural shift highlights a deliberate design choice in CI4: decoupling the query builder's internal state from the raw connection object to encourage cleaner separation of concerns. While it removes the convenience of last_query(), it encourages developers to use dedicated logging mechanisms rather than relying on transient properties that may not exist after execution.

Our network