Title: wpdb::db_connect
Published: April 25, 2014
Last modified: February 24, 2026

---

# wpdb::db_connect( bool $allow_bail = true ): bool

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#wp--skip-link--target)

Connects to and selects database.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#description)󠁿

If `$allow_bail` is false, the lack of database connection will need to be handled
manually.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#parameters)󠁿

 `$allow_bail`booloptional

Allows the function to bail.

Default:`true`

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#return)󠁿

 bool True with a successful connection, false on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#source)󠁿

    ```php
    public function db_connect( $allow_bail = true ) {
    	$this->is_mysql = true;

    	$client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0;

    	/*
    	 * Switch error reporting off because WordPress handles its own.
    	 * This is due to the default value change from `MYSQLI_REPORT_OFF`
    	 * to `MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT` in PHP 8.1.
    	 */
    	mysqli_report( MYSQLI_REPORT_OFF );

    	$this->dbh = mysqli_init();

    	$host    = $this->dbhost;
    	$port    = null;
    	$socket  = null;
    	$is_ipv6 = false;

    	$host_data = $this->parse_db_host( $this->dbhost );
    	if ( $host_data ) {
    		list( $host, $port, $socket, $is_ipv6 ) = $host_data;
    	}

    	/*
    	 * If using the `mysqlnd` library, the IPv6 address needs to be enclosed
    	 * in square brackets, whereas it doesn't while using the `libmysqlclient` library.
    	 * @see https://bugs.php.net/bug.php?id=67563
    	 */
    	if ( $is_ipv6 && extension_loaded( 'mysqlnd' ) ) {
    		$host = "[$host]";
    	}

    	if ( WP_DEBUG ) {
    		mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
    	} else {
    		// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
    		@mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
    	}

    	if ( $this->dbh->connect_errno ) {
    		$this->dbh = null;
    	}

    	if ( ! $this->dbh && $allow_bail ) {
    		wp_load_translations_early();

    		// Load custom DB error template, if present.
    		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(
    			/* translators: 1: wp-config.php, 2: Database host. */
    			__( 'This either means that the username and password information in your %1$s file is incorrect or that contact with the database server at %2$s could not be established. 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 you have typed the correct hostname?' ) . "</li>\n";
    		$message .= '<li>' . __( 'Are you sure the database server is running?' ) . "</li>\n";
    		$message .= "</ul>\n";

    		$message .= '<p>' . sprintf(
    			/* translators: %s: Support forums URL. */
    			__( 'If you are 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/forums/' )
    		) . "</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;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wpdb.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wpdb.php#L1960)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wpdb.php#L1960-L2054)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#related)󠁿

| Uses | Description | 
| [wpdb::parse_db_host()](https://developer.wordpress.org/reference/classes/wpdb/parse_db_host/)`wp-includes/class-wpdb.php` |

Parses the DB_HOST setting to interpret it for mysqli_real_connect().

  | 
| [wp_load_translations_early()](https://developer.wordpress.org/reference/functions/wp_load_translations_early/)`wp-includes/load.php` |

Attempts an early load of translations.

  | 
| [wpdb::bail()](https://developer.wordpress.org/reference/classes/wpdb/bail/)`wp-includes/class-wpdb.php` |

Wraps errors in a nice header and footer and dies.

  | 
| [wpdb::select()](https://developer.wordpress.org/reference/classes/wpdb/select/)`wp-includes/class-wpdb.php` |

Selects a database using the current or provided database connection.

  | 
| [wpdb::init_charset()](https://developer.wordpress.org/reference/classes/wpdb/init_charset/)`wp-includes/class-wpdb.php` |

Sets $this->charset and $this->collate.

  | 
| [wpdb::set_charset()](https://developer.wordpress.org/reference/classes/wpdb/set_charset/)`wp-includes/class-wpdb.php` |

Sets the connection’s character set.

  | 
| [wpdb::set_sql_mode()](https://developer.wordpress.org/reference/classes/wpdb/set_sql_mode/)`wp-includes/class-wpdb.php` |

Changes the current SQL mode, and ensures its WordPress compatibility.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  |

[Show 3 more](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#)

| Used by | Description | 
| [wpdb::check_connection()](https://developer.wordpress.org/reference/classes/wpdb/check_connection/)`wp-includes/class-wpdb.php` |

Checks that the connection to the database is still up. If not, try to reconnect.

  | 
| [wpdb::__construct()](https://developer.wordpress.org/reference/classes/wpdb/__construct/)`wp-includes/class-wpdb.php` |

Connects to the database server and selects a database.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wpdb/db_connect/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.9.0](https://developer.wordpress.org/reference/since/3.9.0/) | $allow_bail parameter added. | 
| [3.0.0](https://developer.wordpress.org/reference/since/3.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwpdb%2Fdb_connect%2F)
before being able to contribute a note or feedback.