tests/Frameworks/WordPress/Version_4_8/wp-includes/wp-db.php
/**
	 * Connect to and select database.
	 *
	 * If $allow_bail is false, the lack of database connection will need
	 * to be handled manually.
	 *
	 * @since 3.0.0
	 * @since 3.9.0 $allow_bail parameter added.
	 *
	 * @param bool $allow_bail Optional. Allows the function to bail. Default true.
	 * @return bool True with a successful connection, false on failure.
	 */
Connect to and select database.
If $allow_bail is false, the lack of database connection will need
to be handled manually.



@param bool $allow_bail Optional. Allows the function to bail. Default true.
@return bool True with a successful connection, false on failure.
Connect to and select database . If $allow_bail is false the lack of database connection will need to be handled manually . @param bool $allow_bail Optional . Allows the function to bail . Default true . @return bool True with a successful connection false on failure .
public function db_connect( $allow_bail = true ) {
		$this->is_mysql = true;
		$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();
			$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;
				$attempt_fallback = true;
				if ( $this->has_connected ) {
					$attempt_fallback = false;
				} elseif ( defined( 'WP_USE_EXT_MYSQL' ) && ! WP_USE_EXT_MYSQL ) {
					$attempt_fallback = false;
				} elseif ( ! function_exists( 'mysql_connect' ) ) {
					$attempt_fallback = false;
				}
				if ( $attempt_fallback ) {
					$this->use_mysqli = false;
					return $this->db_connect( $allow_bail );
				}
			}
		} else {
			if ( WP_DEBUG ) {
				$this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
			} else {
				$this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
			}
		}
		if ( ! $this->dbh && $allow_bail ) {
			wp_load_translations_early();
			if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) {
				require_once( WP_CONTENT_DIR . '/db-error.php' );
				die();
			}
			$message = '<h1>' . __( 'Error establishing a database connection' ) . "</h1>\n";
			$message .= '<p>' . sprintf(
				__( 'This either means that the username and password information in your %1$s file is incorrect or we can&#8217;t contact the database server at %2$s. This could mean your host&#8217;s database server is down.' ),
				'<code>wp-config.php</code>',
				'<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>'
			) . "</p>\n";
			$message .= "<ul>\n";
			$message .= '<li>' . __( 'Are you sure you have the correct username and password?' ) . "</li>\n";
			$message .= '<li>' . __( 'Are you sure that you have typed the correct hostname?' ) . "</li>\n";
			$message .= '<li>' . __( 'Are you sure that the database server is running?' ) . "</li>\n";
			$message .= "</ul>\n";
			$message .= '<p>' . sprintf(
				__( 'If you&#8217;re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.' ),
				__( 'https://wordpress.org/support/' )
			) . "</p>\n";
			$this->bail( $message, 'db_connect_fail' );
			return false;
		} elseif ( $this->dbh ) {
			if ( ! $this->has_connected ) {
				$this->init_charset();
			}
			$this->has_connected = true;
			$this->set_charset( $this->dbh );
			$this->ready = true;
			$this->set_sql_mode();
			$this->select( $this->dbname, $this->dbh );
			return true;
		}
		return false;
	}
public function db_connect ( $ allow_bail = true ) { $ this -> is_mysql = true ; $ 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 ( ) ; $ 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 ; $ attempt_fallback = true ; if ( $ this -> has_connected ) { $ attempt_fallback = false ; } elseif ( defined ( 'WP_USE_EXT_MYSQL' ) && ! WP_USE_EXT_MYSQL ) { $ attempt_fallback = false ; } elseif ( ! function_exists ( 'mysql_connect' ) ) { $ attempt_fallback = false ; } if ( $ attempt_fallback ) { $ this -> use_mysqli = false ; return $ this -> db_connect ( $ allow_bail ) ; } } } else { if ( WP_DEBUG ) { $ this -> dbh = mysql_connect ( $ this -> dbhost , $ this -> dbuser , $ this -> dbpassword , $ new_link , $ client_flags ) ; } else { $ this -> dbh = @ mysql_connect ( $ this -> dbhost , $ this -> dbuser , $ this -> dbpassword , $ new_link , $ client_flags ) ; } } if ( ! $ this -> dbh && $ allow_bail ) { wp_load_translations_early ( ) ; if ( file_exists ( WP_CONTENT_DIR . '/db-error.php' ) ) { require_once ( WP_CONTENT_DIR . '/db-error.php' ) ; die ( ) ; } $ message = '<h1>' . __ ( 'Error establishing a database connection' ) . " </h1> \n " ; $ message .= '<p>' . sprintf ( __ ( 'This either means that the username and password information in your %1$s file is incorrect or we can&#8217;t contact the database server at %2$s. This could mean your host&#8217;s database server is down.' ) , '<code>wp-config.php</code>' , '<code>' . htmlspecialchars ( $ this -> dbhost , ENT_QUOTES ) . '</code>' ) . " </p> \n " ; $ message .= " <ul> \n " ; $ message .= '<li>' . __ ( 'Are you sure you have the correct username and password?' ) . " </li> \n " ; $ message .= '<li>' . __ ( 'Are you sure that you have typed the correct hostname?' ) . " </li> \n " ; $ message .= '<li>' . __ ( 'Are you sure that the database server is running?' ) . " </li> \n " ; $ message .= " </ul> \n " ; $ message .= '<p>' . sprintf ( __ ( 'If you&#8217;re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.' ) , __ ( 'https://wordpress.org/support/' ) ) . " </p> \n " ; $ this -> bail ( $ message , 'db_connect_fail' ) ; return false ; } elseif ( $ this -> dbh ) { if ( ! $ this -> has_connected ) { $ this -> init_charset ( ) ; } $ this -> has_connected = true ; $ this -> set_charset ( $ this -> dbh ) ; $ this -> ready = true ; $ this -> set_sql_mode ( ) ; $ this -> select ( $ this -> dbname , $ this -> dbh ) ; return true ; } return false ; }
Connect to and select database.
Connect to and select database .
/*
		 * Deprecated in 3.9+ when using MySQLi. No equivalent
		 * $new_link parameter exists for mysqli_* functions.
		 */ // 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. /* 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.
		 		 */ // Load custom DB error template, if present. /* translators: 1: wp-config.php. 2: database host */ /* translators: %s: support forums URL */