如何使用Show Tables Query正确检查WordPress数据库中是否存在表

时间:2018-09-09 作者:OmAk

运行此查询时,始终为数据库错误创建日志。当表存在时,它可以正常工作,但当它不存在时,它会生成如下所示的调试日志。

有没有其他方法可以检查数据库表是否存在,或者通过填充调试日志文件来避免这些错误?

if($wpdb->get_var("SHOW TABLES LIKE \'$table_name\'") != $table_name) {
    // do something
}
调试日志:

[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

1 个回复
SO网友:Eric

此代码适用于我,没有“WordPress数据库错误”消息:

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 ) );
}

结束

相关推荐

$wpdb selects wrong database

我在另一个WordPress网站的文件夹中创建了一个新的WordPress网站。示例-主站点。com-mainsite。com/第二站点我创建了一个插件,并使用$wpdb 将数据插入数据库,插件将数据插入第一个/父站点的数据库。我使用的代码包括$wpdb 如下所示:$path = $_SERVER[\'DOCUMENT_ROOT\']; include $path . \'/wp-load.php\'; include $path . \'/wp-config.php\'; inc