Download & Extend

How to see the query that's actually being run

Project:Views
Version:7.x-3.3
Component:User interface
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

There are two places I'm hoping this is possible:

First, in the Views UI - in D6/Views 2, there used to be a preview of the actual SQL code that was going to run. It wasn't always 100% accurate, but it was a start. Is that no longer available anywhere?

Second, I'm using hook_views_query_alter(), and I'd really like to print somewhere (screen, error log, whatever) the query that's being run based on the alterations. I've tried dpm($query), but that gives me the whole krumo tree - all I want is the query - i.e. "select x, y from z where a = 2".

Comments

#1

Status:active» fixed

You can setup the sql preview in the views admin settings, under admin/structure/views/settings.

#2

Status:fixed» active

Thanks. What about the second part of the question - how can I programmatically show the query being run when using hook_views_query_alter()?

#3

can you use dpq() debug function from devel module to print this out for you?

#4

Hi guys,

As I was working on a debugging a views block, its query and custom handlers, I came across this post and wanted to share the little snippet I have been using:

<?php

/**
* Implements of hook_views_pre_execute().
*/
function [MODULE_NAME]_views_pre_execute(&$view){
 
// Debug: print the query with dpq
 
dpq($view->build_info['query']);
}

?>

Place the code in the right file (custom module, for example), according to the Views API (implementation of hook_views_api is required, place hook implementation in custom .module file or .views.inc with a path specified in hook_views_api).
Cache should be flushed after adding the hook implementation to the code.
(Not to mention the Devel module is required for the dpq to work)
 
I have tested the snippet and it seems work as expected.
 
Feel free to let me know if you would have any comments, objections, questions, recommendations or particular concerns on this snippet, I would be glad to provide more information or explain in further details.
I would certainly be happy to see more/better suggestions to find a convenient way of displaying the Views query for debugging.
 
Thanks in advance to all for your feedback, comments, suggestions, testing and reporting.
Cheers!

#5

Status:active» fixed

If you enable the setting (on the settings tab of the Views UI) the actual query will be shown in the live preview. That's sufficient for most people.

#6

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.