• 14

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to display the query executed by the Drupal view

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.

Set the Footer's Input Format to PHP, and paste this snippet into the Footer text:

  $v = views_get_current_view();
  $query = db_prefix_tables(vsprintf($v->build_info['query'], $v->build_info['query_args']));
  $replacements = module_invoke_all('views_query_substitutions', $v);
  $query = str_replace(array_keys($replacements), $replacements, $query);
  echo $query;

For a syntax-highlighted query (using geshifilter.module), use the following snippet:

  require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.pages.inc';
  $v = views_get_current_view();
  $query = db_prefix_tables(vsprintf($v->build_info['query'], $v->build_info['query_args']));
  $replacements = module_invoke_all('views_query_substitutions', $v);
  $query = str_replace(array_keys($replacements), $replacements, $query);
  echo geshifilter_process($query, 'sql');

(Stemmed from @Owen's answer and discussion with @Mech-Software in the comments.)

  • 4
Reply Report

The query exists in the view object. Depending on where you want to use it, you may want to add the variable in a views preprocess function, or the location you're calling the view (if calling it programatically).

If you're just using the default template for it though, you can have access to it there:

// ex. somewhere in your views-view--VIEW_NAME.tpl.php
<?php print db_prefix_tables($view->build_info['query']); ?>

Be careful if your process takes arbitrary SQL though, may be better to call it with the view name, and have it programatically pick up results as required. Or, have a secondary display on your view which returns the result in a XLS result set directly.

  • 3
Reply Report
      • 2
    • This looks more or less what I want but I'm trying to put this into the "footer" vs the view.tps.php. I tried adding globals $view but it returned nothing. This looks promising if I can get it working.
      • 1
    • Woot, I'm almost there, I got it working EXCEPT for the substituion of the form data. What's missing? $current_view = views_get_current_view(); print db_prefix_tables($current_view->build_info['query']); However, the query comes back with [sql clipped] WHERE (node.status <> 0) AND (node.type IN ('%s')) AND (casetracker_case.case_status_id = '%s') ORDER BY node_comment_statistics_last_updated DESC How do I get the %s substitution included?
      • 1
    • Got it working by using the query_args and running that through sprintf. Thanks a ton, you saved me a lot of work.

How to output view queries to the screen.

This works for me in Drupal 7.

go to:


find this function:

 * Generate a query and a countquery from all of the information supplied
 * to the object.
 * @param $get_count
 *   Provide a countquery if this is true, otherwise provide a normal query.
function query($get_count = FALSE) {

just before the very end of the function, use dpq($query);

  // Add all query substitutions as metadata.
  $query->addMetaData('views_substitutions', module_invoke_all('views_query_substitutions', $this));
    return $query;
  • -1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions