28

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();

6 Answers 6

45

Before the query runs:

$this->db->_compile_select(); 

And after it has run:

$this->db->last_query();
Sign up to request clarification or add additional context in comments.

2 Comments

_compile_select() is marked as protected in more recent versions of CodeIgniter (it likely only wasn't declared protected before for PHP4 compatibility). So you could just set the limit to 1 to avoid executing a big query and then call last_query(), or you could use reflection but the first option is preferable.
Also: It looks like get_compiled_select() will be available in a future version of CodeIgniter; it's already in the dev branch (github.com/EllisLab/CodeIgniter/blob/develop/system/database/…). I copied that method to DB_active_rec and it worked fine, even in conjunction with this library: github.com/NTICompass/CodeIgniter-Subqueries
10

Of course, I found it 2 minutes after posting, courtesy of Phil Sturgeon.

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

Comments

8

Also, you can put the following in your controller:

$this->output->enable_profiler(TRUE);

You'll get queries and a lot more.

Comments

1

For anyone finding this old post and wondering what this is in the latest v3 the function is $this->db->get_compiled_select().

Comments

0

In Codeigniter 3, the last query can be obtained via:

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

It is only possible to get the last query when the option save_queries is enabled in application/config/database.php:

/*
|   ['save_queries'] TRUE/FALSE - Whether to "save" all executed queries.
|               NOTE: Disabling this will also effectively disable both
|               $this->db->last_query() and profiling of DB queries.
|               When you run a query, with this setting set to TRUE (default),
|               CodeIgniter will store the SQL statement for debugging purposes.
|               However, this may cause high memory usage, especially if you run
|               a lot of SQL queries ... disable this to avoid that problem.
*/

Source: Codeigniter 3 Query Helper Methods

Comments

-2

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 );
}

1 Comment

This is very unlikely to yield accurate results as to what query is actually being run

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.