How to get last Executed Query in Codeigniter

Codeigniter get last executed query example; In this tutorial you will learn how to get last executed sql query in codeigniter 3 or 4 project. This example show you how to get last executed query using last_query() function of db class in codeigniter application.

In codeigniter simple to use $this->db->last_query() function to print last executed query. This query returns a Query object that represents the last query that was run (the query string, not the result).


public function getLastQuery()
    $query = $this->db->get("products");
    $lastQuery = $this->db->last_query();

    echo "<pre>";


FROM `products`

The query execution happens on all get methods like


While last_query contains the last query which was run


Hope codeignter get last executed query example help you.

Leave a Comment