/** * Checks that the database table exists. Also works on temporary tables (for unit tests mostly). * @global wpdb $wpdb * @param string $table_name with or without $wpdb->prefix * @return boolean */ public static function table_exists($table_name) { global $wpdb, $EZSQL_ERROR; $table_name = EEH_Activation::ensure_table_name_has_prefix($table_name); //ignore if this causes an sql error $old_error = $wpdb->last_error; $old_suppress_errors = $wpdb->suppress_errors(); $old_show_errors_value = $wpdb->show_errors(FALSE); $ezsql_error_cache = $EZSQL_ERROR; $wpdb->get_results("SELECT * from {$table_name} LIMIT 1"); $wpdb->show_errors($old_show_errors_value); $wpdb->suppress_errors($old_suppress_errors); $new_error = $wpdb->last_error; $wpdb->last_error = $old_error; $EZSQL_ERROR = $ezsql_error_cache; if (empty($new_error)) { return TRUE; } else { return FALSE; } }
/** * Checks that the database table exists. Also works on temporary tables (for unit tests mostly). * @global wpdb $wpdb * @param string $table_name with or without $wpdb->prefix * @return boolean */ public static function table_exists($table_name) { global $wpdb, $EZSQL_ERROR; $table_name = EEH_Activation::ensure_table_name_has_prefix($table_name); //ignore if this causes an sql error $old_error = $wpdb->last_error; $old_suppress_errors = $wpdb->suppress_errors(); $old_show_errors_value = $wpdb->show_errors(FALSE); $ezsql_error_cache = $EZSQL_ERROR; $wpdb->get_results("SELECT * from {$table_name} LIMIT 1"); $wpdb->show_errors($old_show_errors_value); $wpdb->suppress_errors($old_suppress_errors); $new_error = $wpdb->last_error; $wpdb->last_error = $old_error; $EZSQL_ERROR = $ezsql_error_cache; //if there was a table doesn't exist error if (!empty($new_error)) { if (in_array(EEH_Activation::last_wpdb_error_code(), array(1051, 1109, 117)) || preg_match('~^Table .* doesn\'t exist~', $new_error)) { return false; } else { //log this because that's weird. Just use the normal PHP error log error_log(sprintf(__('Event Espresso error detected when checking if table existed: %1$s (it wasn\'t just that the table didn\'t exist either)', 'event_espresso'), $new_error)); } } return true; }