为什么即使查询输出正确,$ wpdb-> show_errors()和print_error()仍显示输出?
1 个回答
- 投票数
-
- 2015-02-23
如果满足以下条件,则上面发布的输出是
$wpdb->print_error()
的预期行为-- 您正在运行一个站点,而不是多个站点
-
$wpdb->suppress_errors
设置为false -
$wpdb->show_errors
设置为false
从代码的外观来看,您满足所有这些条件.
还请注意,除非您之前已将其关闭,否则默认情况下
$wpdb->show_errors
设置为true
,因此您无需调用$wpdb->show_errors()
.要仅在发生数据库错误时输出某些内容,您可以执行以下两项操作之一-
1-输出错误并将错误添加到日志中
除了在屏幕上输出之外,
$wpdb->print_error()
方法还将记录您的错误.如果这是理想的行为(推荐),则可以执行此操作-if($wpdb->last_error !== '') : $wpdb->print_error(); endif;
2-输出错误但不记录错误
如果您不希望记录错误,则可以添加自己的
my_print_error()
功能,并使用它代替$wpdb->print_error()
-function my_print_error(){ global $wpdb; if($wpdb->last_error !== '') : $str = htmlspecialchars( $wpdb->last_result, ENT_QUOTES ); $query = htmlspecialchars( $wpdb->last_query, ENT_QUOTES ); print "<div id='error'> <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /> <code>$query</code></p> </div>"; endif; }
最后编辑:语法错误
The output that you posted above is expected behaviour for
$wpdb->print_error()
if the following is true -- You are running a single site, not multisite
$wpdb->suppress_errors
is set to false$wpdb->show_errors
is set to false
From the looks of your code, you meet all those conditions.
Note also that, unless you have turned them off previously,
$wpdb->show_errors
is set totrue
by default, so you don't need to call$wpdb->show_errors()
.To output something only when there is a DB error you can do one of these two things -
1 - Output the error and add the error to the log
As well as outputting on the screen, the
$wpdb->print_error()
method will log your error. If this is desirable behaviour (recommended), you can do this -if($wpdb->last_error !== '') : $wpdb->print_error(); endif;
2 - Output the error but do not log it
If you are not interested in logging the error, you can add your own
my_print_error()
funciton and use that instead of$wpdb->print_error()
-function my_print_error(){ global $wpdb; if($wpdb->last_error !== '') : $str = htmlspecialchars( $wpdb->last_result, ENT_QUOTES ); $query = htmlspecialchars( $wpdb->last_query, ENT_QUOTES ); print "<div id='error'> <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /> <code>$query</code></p> </div>"; endif; }
Last Edit: Syntax Mistake
-
错误消息在$ wpdb-> last_error中,而不在$ wpdb-> last_result中.The error message is in `$wpdb->last_error`, not in `$wpdb->last_result`.
- 2
- 2018-12-12
- Martin_W
为了弄清楚以下问题,请参见 https://wordpress.stackexchange.com/questions/178995/sanitize-a-working-query-string-by-using-wpdb-prepare-fails-with-mysql-db -er 我遇到了一个很奇怪的行为.即使我使用的查询是正确的,并显示正确的输出.
只要我具有
show_errors
和print_error
处于活动状态,我就会得到WP数据库错误输出以及:但是为什么呢?我希望只有在发生错误(例如错误或警告)时才会显示输出,但是这种方式始终显示.