Get last Query in Codeigniter

Sometimes when you are using Activerecord classes and functions in Codeigniter, it is often required to view the original query running in background. This is specially helpful for debugging purposes.

Here is a way to get the query running in background.

Put the below line after running the query you want to debug.

echo $this->db->last_query();

This will give you the last run query in simple sql format. You can copy this query and directly run in your phpmyadmin or other sql editor you are using and debug for what is wrong.