3

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?

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

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.

0
4

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;
-1

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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