Index: src/wp-includes/wp-db.php
===================================================================
--- src/wp-includes/wp-db.php	(revision 27276)
+++ src/wp-includes/wp-db.php	(working copy)
@@ -569,12 +569,15 @@
 			$this->show_errors();
 
 		/* Use ext/mysqli if it exists and:
+		 *  - USE_EXT_MYSQL is defined as false, or
 		 *  - We are a development version of WordPress, or
 		 *  - We are running PHP 5.5 or greater, or
 		 *  - ext/mysql is not loaded.
 		 */
 		if ( function_exists( 'mysqli_connect' ) ) {
-			if ( version_compare( phpversion(), '5.5', '>=' ) || ! function_exists( 'mysql_connect' ) ) {
+			if ( defined( 'USE_EXT_MYSQL' ) ) {
+				$this->use_mysqli = ! USE_EXT_MYSQL;
+			} elseif ( version_compare( phpversion(), '5.5', '>=' ) || ! function_exists( 'mysql_connect' ) ) {
 				$this->use_mysqli = true;
 			} elseif ( false !== strpos( $GLOBALS['wp_version'], '-' ) ) {
 				$this->use_mysqli = true;
@@ -1011,8 +1014,8 @@
 	 * @return string escaped
 	 */
 	function _real_escape( $string ) {
-		if ( $this->dbh ) {
-			if ( $this->use_mysqli ) {
+		if ( $this->dbh || ( $this->use_mysqli && ! $this->dbh->connect_errno ) ) {
+			if ( $this->use_mysqli && ! $this->dbh->connect_errno ) {
 				return mysqli_real_escape_string( $this->dbh, $string );
 			} else {
 				return mysql_real_escape_string( $string, $this->dbh );
@@ -1335,7 +1338,7 @@
 			}
 		}
 
-		if ( ! $this->dbh && $allow_bail ) {
+		if ( ( ! $this->dbh || ( $this->use_mysqli && $this->dbh->connect_errno ) ) && $allow_bail ) {
 			wp_load_translations_early();
 
 			// Load custom DB error template, if present.
@@ -1356,7 +1359,7 @@
 " ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
 
 			return false;
-		} else if ( $this->dbh ) {
+		} else if ( $this->dbh || ( $this->use_mysqli && ! $this->dbh->connect_errno ) ) {
 			$this->set_charset( $this->dbh );
 			$this->set_sql_mode();
 			$this->ready = true;
