WordPress.org

Make WordPress Core

Changeset 38442


Ignore:
Timestamp:
08/30/2016 07:41:11 AM (3 years ago)
Author:
pento
Message:

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.

Location:
branches/4.6
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.6

  • branches/4.6/src/wp-includes/wp-db.php

    r38440 r38442  
    812812            $collate = $this->collate;
    813813        if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) {
     814            $set_charset_succeeded = true;
     815
    814816            if ( $this->use_mysqli ) {
    815817                if ( function_exists( 'mysqli_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
    816                     mysqli_set_charset( $dbh, $charset );
     818                    $set_charset_succeeded = mysqli_set_charset( $dbh, $charset );
    817819                }
    818                 $query = $this->prepare( 'SET NAMES %s', $charset );
    819                 if ( ! empty( $collate ) )
    820                     $query .= $this->prepare( ' COLLATE %s', $collate );
    821                 mysqli_query( $dbh, $query );
     820
     821                if ( $set_charset_succeeded ) {
     822                    $query = $this->prepare( 'SET NAMES %s', $charset );
     823                    if ( ! empty( $collate ) )
     824                        $query .= $this->prepare( ' COLLATE %s', $collate );
     825                    mysqli_query( $dbh, $query );
     826                }
    822827            } else {
    823828                if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
    824                     mysql_set_charset( $charset, $dbh );
     829                    $set_charset_succeeded = mysql_set_charset( $charset, $dbh );
    825830                }
    826                 $query = $this->prepare( 'SET NAMES %s', $charset );
    827                 if ( ! empty( $collate ) )
    828                     $query .= $this->prepare( ' COLLATE %s', $collate );
    829                 mysql_query( $query, $dbh );
     831                if ( $set_charset_succeeded ) {
     832                    $query = $this->prepare( 'SET NAMES %s', $charset );
     833                    if ( ! empty( $collate ) )
     834                        $query .= $this->prepare( ' COLLATE %s', $collate );
     835                    mysql_query( $query, $dbh );
     836                }
    830837            }
    831838        }
Note: See TracChangeset for help on using the changeset viewer.