From 33c1c022e3d7312e0b396d95b210ed12dab55f31 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Tue, 30 Aug 2016 07:42:49 +0000 Subject: [PATCH] Database: Don't force an unsupported character set that previously would've silently failed. [37320] corrected some behaviour in how PHP and MySQL character sets are matched up. This was correct, but had the side effect of causing some incorrectly configured sites to start failing. Prior to [37320], if `DB_CHARSET` was set to `utf8mb4`, but the PHP version didn't support `utf8mb4`, it would fall back to the default character set - usually `latin1`. After [37320], the `SET NAMES` query would force MySQL to treat the connection character set as `utf8mb4`, even if PHP wasn't able to understand it. By checking if `mysqli_set_charset()` succeeded, we can simulate the old behaviour, while maintaining the fix in [37320]. Merge of [38441] to the 4.6 branch. Props danielkanchev fo helping to diagnose this issue. Fixes #37689. Built from https://develop.svn.wordpress.org/branches/4.6@38442 git-svn-id: http://core.svn.wordpress.org/branches/4.6@38383 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/version.php | 2 +- wp-includes/wp-db.php | 27 +++++++++++++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/wp-includes/version.php b/wp-includes/version.php index 8f30218260..429a7459b2 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.6.1-alpha-38440'; +$wp_version = '4.6.1-alpha-38442'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index 931015bdfd..8ebee4c40e 100644 --- a/wp-includes/wp-db.php +++ b/wp-includes/wp-db.php @@ -811,22 +811,29 @@ class wpdb { if ( ! isset( $collate ) ) $collate = $this->collate; if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) { + $set_charset_succeeded = true; + if ( $this->use_mysqli ) { if ( function_exists( 'mysqli_set_charset' ) && $this->has_cap( 'set_charset' ) ) { - mysqli_set_charset( $dbh, $charset ); + $set_charset_succeeded = mysqli_set_charset( $dbh, $charset ); + } + + if ( $set_charset_succeeded ) { + $query = $this->prepare( 'SET NAMES %s', $charset ); + if ( ! empty( $collate ) ) + $query .= $this->prepare( ' COLLATE %s', $collate ); + mysqli_query( $dbh, $query ); } - $query = $this->prepare( 'SET NAMES %s', $charset ); - if ( ! empty( $collate ) ) - $query .= $this->prepare( ' COLLATE %s', $collate ); - mysqli_query( $dbh, $query ); } else { if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) { - mysql_set_charset( $charset, $dbh ); + $set_charset_succeeded = mysql_set_charset( $charset, $dbh ); + } + if ( $set_charset_succeeded ) { + $query = $this->prepare( 'SET NAMES %s', $charset ); + if ( ! empty( $collate ) ) + $query .= $this->prepare( ' COLLATE %s', $collate ); + mysql_query( $query, $dbh ); } - $query = $this->prepare( 'SET NAMES %s', $charset ); - if ( ! empty( $collate ) ) - $query .= $this->prepare( ' COLLATE %s', $collate ); - mysql_query( $query, $dbh ); } } }