How to get last executed query in PHP Codeigniter?
Are you wanted to get last executed sql query in codeigniter 3 project then i will help to get last query in codeigniter. We can get last executed query using last_query() function of db class in codeigniter.
It is a very simple to use $this->db->last_query() function to see SQL statements of last executed query in php codeigniter app. You have to simple code that function after your main query that you wanted check.
Here is a simple controller code and also output for last query:
Example:
public function test_db()
{
$query = $this->db->get("items");
$str = $this->db->last_query();
echo "<pre>";
print_r($str);
exit;
}
Output:
SELECT *
FROM `items`
I hope it can help you...

Hardik Savani
I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- PHP Codeigniter 3 - Create Dynamic Tree View using Bootstrap Treeview JS
- PHP Codeigniter 3 Google Recaptcha Form Validation Example
- How to upload image with validation in PHP Codeigniter?
- How to get last record from MySQL table in Codeigniter?
- How to get last inserted id in Codeigniter?
- How to get current url with query string in codeigniter?
- Codeigniter - How to get current url in controller or view ?
- How to get Ip Address in Codeigniter?