From ac60fcb315d20fb2eb720eaed5928fcd31f19acf Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Thu, 3 Apr 2014 21:58:16 +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. Built from https://develop.svn.wordpress.org/trunk@27935 git-svn-id: http://core.svn.wordpress.org/trunk@27765 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/wp-db.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index 8423731e8c..a2524f152b 100644 --- a/wp-includes/wp-db.php +++ b/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;