How to display the query executed by the Drupal view

Mech Software picture Mech Software · Feb 25, 2010 · Viewed 22k times · Source

I want to display the query that is executed in the drupal view. Currently in the view editor it shows the query however I have a need to use that query in my code to download an excel version of the view.

Is there a way to get the executed query the same way it's shown in the "editor" window of the views menu? I want this at the time the the view is shown.

What I plan to do here is to capture the query in the footer, and have that query posted to a process which will send back an XLS resultset. So i'd like the exact query the view is using to display the results.

Answer

Felix Eve picture Felix Eve · Apr 23, 2014

Or you can use hook_views_pre_execute along with devel's dpq function:

function MY_MODULE_views_pre_execute(&$view) {
  dpq($view->build_info['query']);
}