From ccb4f3714a711c6908a41a2e58c0eb820c9ae1e6 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Thu, 3 Apr 2014 21:57:22 +0000 Subject: [PATCH] Database: Fall back from ext/mysqli to ext/mysql if the connection fails. This allows us to avoid breaking a site that works under ext/mysql but is misconfigured for ext/mysqli. props pento. see #21663. git-svn-id: https://develop.svn.wordpress.org/trunk@27935 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/wp-db.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/wp-includes/wp-db.php b/src/wp-includes/wp-db.php index 8423731e8c..a2524f152b 100644 --- a/src/wp-includes/wp-db.php +++ b/src/wp-includes/wp-db.php @@ -548,6 +548,15 @@ class wpdb { */ private $use_mysqli = false; + /** + * Whether we've managed to successfully connect at some point + * + * @since 3.9.0 + * @access private + * @var bool + */ + private $has_connected = false; + /** * Connects to the database server and selects a database * @@ -1336,6 +1345,26 @@ class wpdb { if ( $this->dbh->connect_errno ) { $this->dbh = null; + + /* It's possible ext/mysqli is misconfigured. Fall back to ext/mysql if: + * - We haven't previously connected, and + * - USE_EXT_MYSQL isn't set to false, and + * - ext/mysql is loaded. + */ + $attempt_fallback = true; + + if ( $this->has_connected ) { + $attempt_fallback = false; + } else if ( defined( 'USE_EXT_MYSQL' ) && ! USE_EXT_MYSQL ) { + $attempt_fallback = false; + } else if ( ! function_exists( 'mysql_connect' ) ) { + $attempt_fallback = false; + } + + if ( $attempt_fallback ) { + $this->use_mysqli = false; + $this->db_connect(); + } } } else { if ( WP_DEBUG ) { @@ -1367,6 +1396,7 @@ class wpdb { return false; } else if ( $this->dbh ) { + $this->has_connected = true; $this->set_charset( $this->dbh ); $this->set_sql_mode(); $this->ready = true;