+ /*
+ * Deprecated in 3.9+ when using MySQLi. No equivalent
+ * $new_link parameter exists for mysqli_* functions.
+ */
+ $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true;
+ $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0;
+
+ if ( $this->use_mysqli ) {
+ $this->dbh = mysqli_init();
+
+ // mysqli_real_connect doesn't support the host param including a port or socket
+ // like mysql_connect does. This duplicates how mysql_connect detects a port and/or socket file.
+ $port = null;
+ $socket = null;
+ $host = $this->dbhost;
+ $port_or_socket = strstr( $host, ':' );
+ if ( ! empty( $port_or_socket ) ) {
+ $host = substr( $host, 0, strpos( $host, ':' ) );
+ $port_or_socket = substr( $port_or_socket, 1 );
+ if ( 0 !== strpos( $port_or_socket, '/' ) ) {
+ $port = intval( $port_or_socket );
+ $maybe_socket = strstr( $port_or_socket, ':' );
+ if ( ! empty( $maybe_socket ) ) {
+ $socket = substr( $maybe_socket, 1 );
+ }
+ } else {
+ $socket = $port_or_socket;
+ }
+ }
+
+ if ( WP_DEBUG ) {
+ mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
+ } else {
+ @mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
+ }
+
+ 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
+ * - WP_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( 'WP_USE_EXT_MYSQL' ) && ! WP_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();
+ }
+ }