Getting raw SQL Queries in CodeIgniter 1.7

Viewed 35723

I'm trying to debug some code in my first serious CodeIgniter app and I can't seem to find where I can simply get the raw SQL that my ActiveRecord code just generated.

    $where  = 'DAY(`datetime_start`) = '. date('d',$day) .' AND ';
    $where .= 'MONTH(`datetime_start`) = '. date('m',$day) .'';

    $this->db->from('events')->where($where);
    $result = $this->db->get();

Thanks for the help!

5 Answers

You can make something like this until CI's next version is released

private function get_query_string()
{
    return  'SELECT ' . 
            implode( ' , ' , $this->db->ar_select ) .
            ' FROM ' .
            implode( ' , '  , $this->db->ar_from ) .
            ( count( $this->db->ar_where ) ? ' WHERE ' : '' ) .
            implode( ' ' , $this->db->ar_where );
}
Related