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
2

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 ) );
}
1
  • It's basically the same code.. – Tofandel Oct 7 '19 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.