PinoyTech.org

CodeIgniter, Kohana, Mootools, jQuery and CSS

How to Debug Queries with CodeIgniter

Posted by teejay on October 31, 2009

If you are like me - browsing through the forums almost everyday - you'll probably notice the large number of questions regarding SQL debugging. I try to answer a few depending on the time I have.

Eventually, you get tired. That's the reason I posted this. There are two ways in CodeIgniter to extract the query and debug it using a GUI SQL Manager.

Using the CodeIgniter Profiler

CodeIgniter has a built-in profiler that tries to extract information for each page load. It also lists all the queries that were done to generate the page. From there you can identify if the certain query was generated correctly.

Using the Active Records Last Query Function

I use this a lot. Whenever I suspect that the query generated was incorrect, I put echo $this->db->last_query(); below the script to see the query generated. I check it out. If I can't figure out the problem, I copy and paste this in my GUI SQL Manager and check if it generates the correct information I need.

$this->select('students.name, students.age, schools.name');
$this->db->join('students_schools', 'students_schools.student_id = students.id', 'LEFT');
$this->db->join('schools', 'students_schools.school_id = schools.id', 'LEFT');
$this->db->get_where('students', array('students.id' => 17));

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

Debugging CodeIgniter generated queries are so much easier if you use these techniques. I sure hope this post could help anyone who is using CodeIgniter and have problems debugging his queries.

Categories: How To, Web Development

Tags: codeigniter, php

1 Comments

rodrigo

Thnxs a lot, I’m sure i’ll make use of this function a lot !

December 6th 2009

Comments