最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mysql - How to properly check if a table exists in WordPress Database using Show Tables Query

programmeradmin4浏览0评论

This query when runs always create logs for Database error. It works fine when table exists, but when it does not it generates the below shown debug logs.

Any alternative way to check if database table exists or to avoid these errors from filling up the debug logs file?

if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
    // do something
}

Debug Logs:

[09-Sep-2018 12:21:50 UTC] WordPress database error Table 'splivemain_db.wp_survey_popup_form' doesn't exist for query DESCRIBE wp_survey_popup_form; made by do_action('omxl-logger_page_omxl-cf7'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, myplugin__Logger->display, myplugin__Logger->get_columns

[09-Sep-2018 12:21:50 UTC] WordPress database error Table 'splivemain_db.wp_survey_popup_form' doesn't exist for query DESCRIBE wp_survey_popup_form; made by do_action('omxl-logger_page_omxl-cf7'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, myplugin__Logger->display, myplugin__Logger->get, myplugin__Logger->get_columns

[09-Sep-2018 12:21:50 UTC] WordPress database error Table 'splivemain_db.wp_survey_popup_form' doesn't exist for query SELECT* FROM wp_survey_popup_form WHERE 1=1 ORDER BY activity_date DESC LIMIT 0, 10 made by do_action('omxl-logger_page_omxl-cf7'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, myplugin__Logger->display, myplugin__Logger->get

This query when runs always create logs for Database error. It works fine when table exists, but when it does not it generates the below shown debug logs.

Any alternative way to check if database table exists or to avoid these errors from filling up the debug logs file?

if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
    // do something
}

Debug Logs:

[09-Sep-2018 12:21:50 UTC] WordPress database error Table 'splivemain_db.wp_survey_popup_form' doesn't exist for query DESCRIBE wp_survey_popup_form; made by do_action('omxl-logger_page_omxl-cf7'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, myplugin__Logger->display, myplugin__Logger->get_columns

[09-Sep-2018 12:21:50 UTC] WordPress database error Table 'splivemain_db.wp_survey_popup_form' doesn't exist for query DESCRIBE wp_survey_popup_form; made by do_action('omxl-logger_page_omxl-cf7'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, myplugin__Logger->display, myplugin__Logger->get, myplugin__Logger->get_columns

[09-Sep-2018 12:21:50 UTC] WordPress database error Table 'splivemain_db.wp_survey_popup_form' doesn't exist for query SELECT* FROM wp_survey_popup_form WHERE 1=1 ORDER BY activity_date DESC LIMIT 0, 10 made by do_action('omxl-logger_page_omxl-cf7'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, myplugin__Logger->display, myplugin__Logger->get
Share Improve this question asked Sep 9, 2018 at 12:27 OmAkOmAk 182 silver badges8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

This code is working for me with no "WordPress database error" messages:

if ( $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) ) === $table_name ) {
    error_log( 'Table exists' . ': ' . print_r( $table_name, true ) );
} else {
    error_log( 'Table does not exist' . ': ' . print_r( $table_name, true ) );
}
发布评论

评论列表(0)

  1. 暂无评论