In this post, I would like to share how to get or echo the last executed query in CodeIgniter application, Sometimes in your application, you want to debug and get the last executed query, you can get the last executed query using last_query() function in CodeIgniter. In CodeIgniter application you use the query builder with a group by, order by limit functions that time you don't get the whole query, to get the full query use the function last_query();

The below example demonstrates how to print the last query.

Home_Model.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Home_Model extends CI_Model {

    public function get_all_users() {
        $result = $this->db->get("users")->result();
        echo $this->db->last_query();
        exit;
        return $result;
  }

?>

Output:

SELECT * FROM `users`
In get_all_users() function, we have printed the last query, which will print the query in sql format.

Written by
Check out this Author's contributed articles.
If you like Tutorialsmint and would like to contribute, you can also write an article using Write an Article. See your article appearing on the Tutorialsmint main page and help other programmers to enhance their knowledge.

Comments(0)

RECENT TUTORIALS
Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Wed Dec 26 • 08:00 AM GMT
Edit Post
Add Reply