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).

Solution:

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

    echo "<pre>";
    print_r($lastQuery);
    die;
}

Result:

SELECT *
FROM `products`

The query execution happens on all get methods like

$this->db->get('table_name');
$this->db->get_where('table_name',$array);

While last_query contains the last query which was run

$this->db->last_query();

Hope codeignter get last executed query example help you.

Leave a Comment