CodeIgniter Forums
View query before being executed? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter Archived (from EllisLab) (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: View query before being executed? (/thread-11337.html)



View query before being executed? - El Forum - 09-04-2008

[eluser]jrlooney[/eluser]
Hello,

How can I dump the query that $this->db is going to execute so that I can review it?

So, for example, for this query, I'd like to see the SQL (even if I just var_dump it):

Code:
$this->db->limit(0,$this->per_page);
$this->db->select('contact_id,first_name,last_name');
$data['query'] = $this->db->get('cm_contact');

thanks in advance


View query before being executed? - El Forum - 09-04-2008

[eluser]jrlooney[/eluser]
nevermind. found it.
$this->db->last_query();


View query before being executed? - El Forum - 09-05-2008

[eluser]phpoet[/eluser]
According to the documentation, last_query() returns the sql for the last query that has already been executed. It pops the last query off the queries[] array from the CI_DB_Driver class and it looks like the query doesn't get added to the array until it's run.


View query before being executed? - El Forum - 09-05-2008

[eluser]xwero[/eluser]
There is a method _select_compile that lets you show the query before executing but as you can gather from the name it only works for the select statements.