Home » Php » php – Getting CodeIgniter Active Record's current SQL code

php – Getting CodeIgniter Active Record's current SQL code

Posted by: admin April 23, 2020 Leave a comment

Questions:

for example, i want to convert this;

$this->db->get('table');

to this;

'SELECT * FROM table'

is there any function for this? i searched on the user guide of CI but didnt find any solution.

How to&Answers:

You can also use $this->db->get_compiled_select(). The difference between get_compiled_select() and last_query() is that get_compiled_select() gives the query string generated even if you don’t run the query against the database.

Answer:

Try

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

after you run your Active Record Query and it’ll spit out the raw SQL it ran for you. I use it often.