如何在执行后立即打印被切除的sql
-
-
我知道已经为时已晚,但仅供将来参考.您可以在将其传递给查询之前回显prepare语句.肯定会更容易.I know it's too late, but for future reference. You can just echo prepare statement before passing it to query. It would be surely easier.
- 1
- 2016-10-20
- Maciej Paprocki
-
4 个回答
- 投票数
-
- 2013-08-16
$wpdb
对象具有为此设置的一些属性:global $wpdb; // Print last SQL query string echo $wpdb->last_query; // Print last SQL query result echo $wpdb->last_result; // Print last SQL query Error echo $wpdb->last_error;
注意:首先,您必须在WordPress根文件夹的
define( 'SAVEQUERIES', true );
文件中设置wp-config.php
./p>The
$wpdb
object has some properties getting set for that:global $wpdb; // Print last SQL query string echo $wpdb->last_query; // Print last SQL query result echo $wpdb->last_result; // Print last SQL query Error echo $wpdb->last_error;
Note: First of all you have to set
define( 'SAVEQUERIES', true );
in yourwp-config.php
file at root folder of WordPress.-
嗯,但在我的情况下,$ wpdb-> last_query中没有任何内容.hmm but in my case there is nothing in $wpdb->last_query.
- 0
- 2013-08-16
- ravisoni
-
你在`wp-config.php`或类似的东西中定义了('SAVEQUERIES',true);`吗?在您的脚本中是否为define('SAVEQUERIES')和Defined('SAVEQUERIES',true);`?否则它将无法正常工作.Have you `defined( 'SAVEQUERIES', true );` in your `wp-config.php` or something like `! defined( 'SAVEQUERIES' ) AND defined( 'SAVEQUERIES', true );` in your script? Else it won't work.
- 0
- 2013-08-16
- kaiser
-
是的,我有,我认为查询根本没有运行,因为没有设置$ wpdb-> last_query.:(Yes i have, I think the query is not running at all that y there is nothing setting is $wpdb->last_query. :(
- 0
- 2013-08-16
- ravisoni
-
然后打开wp_debug,这样您就会收到错误或警告(如果有的话).turn on wp_debug then, so that you'll get errors or warning if any there.
- 1
- 2013-08-16
- Kumar
-
WordPress数据库错误:[查询为空]WordPress database error: [Query was empty]
- 0
- 2013-08-16
- ravisoni
-
改用`$ wpdb->get_results()`,看看`$ wpdb`上的Codex文档.Try `$wpdb->get_results()` instead and take a look at the Codex documentation on `$wpdb`.
- 0
- 2013-08-16
- kaiser
-
我知道您的原始查询来自很久以前,但是听起来您正在遇到列大小问题.仅针对正在寻找无查询结果且无错误的解决方案的任何其他人-警告,当查询中的列超过数据库中列的大小时,wpdb会以无提示或错误的方式静默退出.几乎没有办法看到这种情况的发生,并且WordPress一直对此进行粗心的抵抗(IMHO).您可以在开发环境中将简短的补丁放入wpdb.php中,以使查看起来更加容易.I know your original query was from a long time ago, but it sounds like you were hitting the column size issue. Just for anyone else who is searching for a solution to no query results with no error - be warned that wpdb exits silently, with no message or error, when a column in your query exceeds the size of the column in your database. There is almost no way to see this has happened, and WordPress have been carelessly resistant (IMHO) to fixing this. There is a short patch you can put into wpdb.php in a dev environment to make seeing this much easier.
- 1
- 2019-12-22
- Brian C
-
@BrianC您可能想链接到补丁?甚至更好:将其添加到答案中?还是[编辑]这个答案?@BrianC You might want to link to the patch? Or even better: Add it in an answer? Or [edit] this answer?
- 0
- 2019-12-26
- kaiser
-
- 2013-08-16
我在这里列出了3种方法:
- 使用
SAVEQUERIES
并在页脚中打印所有查询 - 使用
$wpdb->last_query
仅打印执行的最新查询,这对于调试功能很有用. - 使用类似查询监视器的插件.
您需要将其添加到wp-config.php
define('SAVEQUERIES', true);
然后在主题的页脚中添加以下代码:
<?php if (current_user_can('administrator')){ global $wpdb; echo "<pre>Query List:"; print_r($wpdb->queries); echo "</pre>"; }//Lists all the queries executed on your page ?>
或者,如果您只想打印最后执行的查询,则可以在
$wpdb
查询函数调用下面使用它.global $wpdb; echo $wpdb->last_query;//lists only single query
第三种方法是使用诸如Query Monitor之类的插件,该插件详细列出了在页面上执行的所有查询,以及与之相关的其他详细信息,例如返回的行数和执行时间,或者它是否很慢查询. http://wordpress.org/plugins/query-monitor/
仅在DEV环境中使用此插件是个好主意,不应在活动站点上使其保持激活状态.另外,查询监视器有时可能会导致您的页面出现问题,例如,如果错误过多,则模板/页面上的5XX错误.
I've listed down 3 approaches in here:
- Using
SAVEQUERIES
and printing all the queries in footer - Using
$wpdb->last_query
to print just the latest query executed, this is useful for debugging functions. - Using a plugin like Query Monitor.
You'd need to add this in your wp-config.php
define('SAVEQUERIES', true);
Then in the footer of your theme add this code:
<?php if (current_user_can('administrator')){ global $wpdb; echo "<pre>Query List:"; print_r($wpdb->queries); echo "</pre>"; }//Lists all the queries executed on your page ?>
Or if you'd like to print just the last executed query, you can use this just below your
$wpdb
query function call.global $wpdb; echo $wpdb->last_query;//lists only single query
A 3rd approach would be to use a plugin like Query Monitor which lists all the queries executed on a page in detail, and other details associated with it like how many rows it returns and the time taken for execution or if it's a slow query. http://wordpress.org/plugins/query-monitor/
It's a good idea to use this plugin in DEV environment only and shouldn't be left activated on a live site. Also, Query Monitor can sometimes cause issues with your page, Like 5XX error on your template/page if there are too many errors.
-
- 2017-04-15
You have to add both functions,otherwise it will never show error
$wpdb->show_errors(); $wpdb->print_error();
This function will show you proper error like this this
-
- 2017-07-04
我想补充一点,@ kaiser投票最好的答案并不完全正确:
// Print last SQL query string $wpdb->last_query
返回的是 ARRAY ,而不是字符串.因此,要输出最后一个查询,您应该这样做:
echo 'Last query: '.var_export($wpdb->last_query, TRUE);
I wanted to add that the best up-voted answer by @kaiser is not fully correct:
// Print last SQL query string $wpdb->last_query
The return of it is ARRAY, not a string. So to output last query you should do this:
echo 'Last query: '.var_export($wpdb->last_query, TRUE);
我正在寻找一种可以在:之后打印执行的sql查询的方法.
如果我能看到查询中正在显示什么值,那就太好了.
谢谢