Sometimes, it is useful to know about the last query that was run against the database, either for debugging purposes or for reasons where you wish to have an audit trail of every interaction with the database—you'll be surprised at the number of times you'll need to do this. CodeIgniter comes with a really handy function that you can use to write out the most recent query that CodeIgniter sent to the database.
Add or adapt the following line of code into your controller or model:
$this->db->last_query();
Quite simply, this function will return the last query to be sent to the database; you can place it in the controller or model (even the view if you wish, but it's better to keep it at the logical side of your application rather than the view). It will return the query that you can use as an audit in the form of a string; for example, consider the following line of code:
log_message('level', $this->db->last_query())
The...