如何显示查询中运行的SQL查询?
4 个回答
- 投票数
-
- 2010-12-03
您好 @Keith Donegan:
如果我正确理解了您的问题,我认为这就是您想要的吗?
<?php echo $GLOBALS['wp_query']->request; ?>
$wp_query
是一个全局变量,它包含循环运行的当前查询.如果您在循环仍处于活动状态时甚至在循环之后立即运行上述代码,则应该为您提供循环中的SQL.只需确保检查一下,然后再运行其他使用query_posts()
的东西即可.Hi @Keith Donegan:
If I understand your question correctly I think this is what you are looking for?
<?php echo $GLOBALS['wp_query']->request; ?>
$wp_query
is a global variable that contains the current query run by the loop. If you run the above code anytime while the loop is still active or even right after the loop it should give you the SQL from the loop. Just make sure you inspect it before letting something else run that usesquery_posts()
again.-
如何获取`$ wpdb`的查询?$ GLOBALS ['wpdb']-> request`不起作用How to get queries of `$wpdb`? `$GLOBALS['wpdb']->request` not working
- 0
- 2017-01-21
- mpsbhat
-
即使在自定义查询中也可以使用, `$my_query=new WP_Query([/* ... some args ... */]);`=>`$my_query-> request`Works even on custom query, `$my_query = new WP_Query([ /* ...some args... */ ]);` => `$my_query->request`
- 2
- 2017-08-16
- jave.web
-
-
- 2010-12-03
查看此答案:最佳您的functions.php文件的代码集合
然后将?debug=sql添加到任何WP URL,它将输出已运行查询的完整列表.(是的,这很可怕...)
See this answer: Best Collection of Code for your functions.php file
Then add ?debug=sql to any WP URL, and it'll output the full list of queries that were run. (And yes, it's scary...)
-
- 2010-12-03
如果您仅对循环感兴趣,这就是我通常使用的方法:
add_filter( 'posts_request', 'dump_request' ); function dump_request( $input ) { var_dump($input); return $input; }
If you are only interested in Loops this is what I usually use:
add_filter( 'posts_request', 'dump_request' ); function dump_request( $input ) { var_dump($input); return $input; }
在显示确切的SQL代码之前,我遇到了一个函数. 例如,在一个循环中,但记不清了.
有人可以告诉我该功能吗?