35

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.

2 Answers 2

61

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I just realized that if you use _compile_select(), that you have to use $this->db->_reset_select(); after executing the query. If you don't, CodeIgniter won't clear the query, and all queries onward won't work.
_compile_select() is not working anymore because they protected the function at the latest CI2 Github repo. I used this function so many times and now I'm stuck. Maybe there is a solution?
After searching CI commit history, I just saw that there is a new public method; get_compiled_select(). codeigniter.com/nightly_user_guide/database/…
Fatal error: Call to undefined method CI_DB_mysql_driver::get_compiled_select()
32

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.