I've got a Active Record query which i need to grab the sql from, so i'm doing the query and then i'm doing for example

$query = $this->EE->db->select('foo')->get('bar');     
$bob = $this->EE->db->last_query();

Now, on my local machine it picks it up fine. But, on the live site it's picking up a query of the exp_sites table and basically just returns the contents of the exp_sites table for this site (it's not msm so there is only one row). That's not the query i want, so how is it getting picked up there?!?!

Anyone know why this might be? Caching? A full moon?

  • Is DB caching enabled on the live site? Commented Apr 25, 2013 at 13:09
  • No, it doesn't look like it. I'm sure i've seen this before but cannot remember the fix. Gaaah! Commented Apr 25, 2013 at 13:13

3 Answers 3


Put this above your query.

$this->EE->db->save_queries = TRUE;

EE_Config.php sets the save_queries value dynamically based on debug/profiler settings, ignoring settings in database.php. This is why it worked fine in your local environment.


This will do it:

$this->EE->db->save_queries = TRUE;
$query = $this->EE->db->select('foo')->get('bar');
$bob = end($this->EE->db->queries);
$this->EE->db->save_queries = FALSE;

As far as I know, last_query() just doesn't work in EE... it never has (or at least when I've tried to use it)

easiest way to do it (which is what I do) is

$query = $this->EE->db->select('foo')->get('bar');
$bob = $query->result_array();

Someone infinitely more intelligent than I will probably be able to tell you why it doesn't work, but suffice to say - I gave up trying to figure it out.

  • I want the compiled sql though, not the result_array() in this scenario. Thanks though!! Commented Apr 25, 2013 at 13:23
  • Ahhh OK - So in that case try doing $this->EE->db->cache_off() before your $query and see what happens? Commented Apr 25, 2013 at 13:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.