ItSolutionStuff.com

How to Get Last Executed Query in Codeigniter?

By Hardik Savani • November 5, 2023
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

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Codeigniter Create Dynamic Tree View using Treeview JS

Read Now →

Codeigniter Google Recaptcha Form Validation Example

Read Now →

How to implement and use DataTables in CodeIgniter?

Read Now →

Codeigniter Drag and Drop Multiple Image Upload Example

Read Now →

Codeigniter Dynamic Dependent Dropdown using Ajax Example

Read Now →

How to Implement Flash Messages in Codeigniter?

Read Now →

Codeigniter Ajax Infinite Scroll Pagination Example

Read Now →

Codeigniter Image Upload with Validation Example

Read Now →

How to Get Last Record in Codeigniter?

Read Now →

How to Get Last Inserted ID in Codeigniter?

Read Now →

Codeigniter Generate PDF from View using Dompdf Example

Read Now →

How to Get Current URL with Query String in Codeigniter?

Read Now →

How to Get Current URL in Codeigniter?

Read Now →

How to Get IP Address in Codeigniter?

Read Now →