Skip to content

Instantly share code, notes, and snippets.

@briancoogan

briancoogan/db.php

Last active Aug 28, 2020
Embed
What would you like to do?
WordPress wp-db.php tweak to display errors
<?php
// db.php: Cintix/BC debug extended version of wp-db class
//
// Tiny extension to wp-db.php to detect the normally silent length
// overrun error, and return it so the developer can see the error.
// Without this, wp-db.php just returns with no errors and no rows changed;
// which is a nightmare to debug.
//
// Overrides only a single wpdb method process_fields() so it returns
// noisy errors on length overruns
//
// To install, just copy this file db.php into wp-contents/db.php
// To uninstall, just remove wp-contents/db.php - will revert to just using
// the default wpdb class from wp-includes/wp-db.php
//
// Should automatically pick up and work with new versions of wp-db.php
// provided the process_fields() function isn't modified (and it hasn't
// changed since at least 5.2.3 days, ie: April 2016, probably earlier).
// Expecting this to be fairly stable over time (Aug 2020);
// the process_fields function is small so easy to check for changes.
//
// This was taken from a wp-db.php initially done in 2012 to add null
// processing, since been added to core.
// For further details, see:
// https://stackoverflow.com/a/10349255/1495084
//
// BrianC / Cintix public domain 27 Aug 2020
// StackOverflow user:1495084
// setup dummy $wpdb to prevent it from being instantiated
$wpdb = new stdclass();
// ABSPATH . WPINC . "/wp-db.php"; -- already included by core
class wpdb_debug_length extends wpdb
{
// --------------------------------------------------------------
// This only overrides process_fields()
// apart from comments, process_fields() hasn't changed in at least
// 4 years, since at least 4.5.0 (12 April 2016)
// (I didn't check previously)
// --------------------------------------------------------------
/**
* Processes arrays of field/value pairs and field formats.
*
* This is a helper method for wpdb's CRUD methods, which take field/value pairs
* for inserts, updates, and where clauses. This method first pairs each value
* with a format. Then it determines the charset of that field, using that
* to determine if any invalid text would be stripped. If text is stripped,
* then field processing is rejected and the query fails.
*
* @since 4.2.0
*
* @param string $table Table name.
* @param array $data Field/value pair.
* @param mixed $format Format for each field.
* @return array|false An array of fields that contain paired value and formats.
* False for invalid values.
*/
protected function process_fields( $table, $data, $format ) {
$data = $this->process_field_formats( $data, $format );
if ( false === $data ) {
return false;
}
$data = $this->process_field_charsets( $data, $table );
if ( false === $data ) {
return false;
}
$data = $this->process_field_lengths( $data, $table );
if ( false === $data ) {
return false;
}
$converted_data = $this->strip_invalid_text( $data );
if ( $data !== $converted_data ) {
// Start inserted code BC ==============================
$problem_fields = array();
foreach ( $data as $field => $value ) {
if ( $value !== $converted_data[ $field ] ) {
$a = print_r($value, 1);
$problem_fields[] = $field."=$a";
}
}
if ( count( $problem_fields ) === 1 ) {
$message = __( 'WordPress Database Error: Processing the value for the following field failed, the supplied value may have been too long or contained invalid data: %s.' );
} else {
$message = __( 'WordPress Database Error: Processing the values for the following fields failed, the supplied values may have been too long or contained invalid data: %s.' );
}
$this->last_error = sprintf( $message, implode( ', ', $problem_fields ) );
// End inserted code BC ==============================
return false;
}
return $data;
}
}
$wpdb = new wpdb_debug_length(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
?>
@briancoogan

This comment has been minimized.

Copy link
Owner Author

@briancoogan briancoogan commented Aug 28, 2020

Quick notes

This is a slightly tweaked version of WordPress's wp-db class, which warns you when you overrun a field length.
Formerly, wp-db would just ignore changes without error (though with a 0 return for rows changed).

To install, simply copy dp.php to wp-content/db.php:
cp db.php wp-content/db.php
When present, the db.php file overrides wp-db.php.

To uninstall, simply:
rm -f wp-content/db.php

There should be no change in operation other than the additional errors, and new core versions of wp-db.php should automatically be picked up and used, bar the one function which is overridden.

This idea came from code shared by Liam Murphy here.

If anything breaks, simply uninstall as above.

Please let me know if you find any problems!

BrianC
Aug 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment