WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#32029 closed defect (bug) (fixed)

WP_Error( 'wpdb_get_table_charset_failure' ) thrown when doing "SHOW TABLES LIKE %s"

Reported by: datafeedr.com Owned by: pento
Milestone: 4.2 Priority: high
Severity: normal Version: 4.2
Component: Database Keywords: has-patch
Focuses: Cc:

Description

When attempting to check if a DB table exists, the "wpdb_get_table_charset_failure" error is being thrown. Here's where I believe the error is being thrown (wp-includes/wp-db.php:2235~2238):

		$results = $this->get_results( "SHOW FULL COLUMNS FROM `$table`" );
		if ( ! $results ) {
			return new WP_Error( 'wpdb_get_table_charset_failure' );
		}

My PHP error log has "WordPress database error Table 'private.wp_dfrpswc_temp_post_ids_by_set_id' doesn't exist for query SHOW FULL COLUMNS FROM wp_dfrpswc_temp_post_ids_by_set_id".

I believe the issue is related to the get_table_charset() method being called before the new table has been created.

I'm running WP 4.2-RC1-32175. This is my custom code which generates the error:

$table_name = $wpdb->prefix . 'dfrpswc_temp_post_ids_by_set_id';
$query = $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name );
if ( $wpdb->get_var( $query ) != $table_name ) {
	dfrpswc_create_table();
}

Let me know if you need any additional information.

Thanks
Eric

Attachments (1)

32029.diff (2.4 KB) - added by pento 5 years ago.

Download all attachments as: .zip

Change History (17)

#1 @pento
5 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to 4.2
  • Priority changed from normal to high

This ticket was mentioned in Slack in #core by drew. View the logs.


5 years ago

#3 @netweb
5 years ago

#32033 was marked as a duplicate.

@pento
5 years ago

#4 follow-up: @pento
5 years ago

  • Keywords has-patch needs-testing added; needs-patch removed

32029.diff doesn't try and check the collation of tables when it's a query that really doesn't need it.

@datafeedr.com: Would you be able to test this patch against your code, please?

#5 follow-up: @valendesigns
5 years ago

  • Keywords needs-testing removed

I ran into this very same thing earlier today with one of my plugins. Even though this is definitely an issue that should be solved in Core, I also think there is a better way to see if a table exists. Try the following instead:

$table_name = $wpdb->prefix . 'dfrpswc_temp_post_ids_by_set_id';
if ( ! in_array( $table_name, $wpdb->tables() ) ) {
	dfrpswc_create_table();
}

@pento I tested the patch and can confirm it solves the issue.

#6 in reply to: ↑ 5 @nacin
5 years ago

Replying to valendesigns:

$table_name = $wpdb->prefix . 'dfrpswc_temp_post_ids_by_set_id';
if ( ! in_array( $table_name, $wpdb->tables() ) ) {
	dfrpswc_create_table();
}

Nope. $wpdb->tables() isn't based on the database. This will never return true.

#7 @valendesigns
5 years ago

That sucks! Well, disregard the comment. Patch works in any case.

#8 @pento
5 years ago

  • Owner set to pento
  • Resolution set to fixed
  • Status changed from new to closed

In 32232:

WPDB: When deciding if a query needs extra sanity checking based on collation, we can quickly return if it's a query that will never return user data.

Fixes #32029.

#9 @pento
5 years ago

In 32233:

WPDB: When deciding if a query needs extra sanity checking based on collation, we can quickly return if the query is entirely ASCII characters.

See #32029.

#10 @pento
5 years ago

In 32234:

WPDB: When deciding if a query needs extra sanity checking based on collation, return early when we can. Merges [32232] and [32233] to the 4.1 branch.

See #32029.

#11 @pento
5 years ago

In 32235:

WPDB: When deciding if a query needs extra sanity checking based on collation, return early when we can. Merges [32232] and [32233] to the 4.0 branch.

See #32029.

#12 @pento
5 years ago

In 32239:

WPDB: When deciding if a query needs extra sanity checking based on collation, return early when we can. Merges [32232] and [32233] to the 3.9 branch.

See #32029.

#13 @pento
5 years ago

In 32240:

WPDB: When deciding if a query needs extra sanity checking based on collation, return early when we can. Merges [32232] and [32233] to the 3.8 branch.

See #32029.

#14 @pento
5 years ago

In 32241:

WPDB: When deciding if a query needs extra sanity checking based on collation, return early when we can. Merges [32232] and [32233] to the 3.7 branch.

See #32029.

This ticket was mentioned in Slack in #core by dd32. View the logs.


5 years ago

#16 in reply to: ↑ 4 @datafeedr.com
5 years ago

Replying to pento:

@datafeedr.com: Would you be able to test this patch against your code, please?

@pento The patch works for me, too. Thanks for the fix!

Eric

Note: See TracTickets for help on using tickets.