wpdb::db_connect( bool $allow_bail = true ): bool
- Since
- 3.0.0, 3.9.0
- Source
wp-includes/class-wpdb.php:1954
Description
If $allow_bail is false, the lack of database connection will need to be handled manually.
Compatibility
- WordPress
- since 3.9.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$allow_bailbooloptional- Allows the function to bail. Default true.Default:
true
Return value
bool- True with a successful connection, false on failure.
Performance profile
How much work a call to wpdb::db_connect() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 55–146
- Plugin surface
- None
- Called by
- 2
Reads or writes the filesystem via file_exists().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 191.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- filesystemfilesystem access
file_exists()called directly
Further down the call graph this can also reach hook, query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wpdb::db_connect() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 55–74 | mysqli_report(), mysqli_init(), ->parse_db_host(), mysqli_real_connect() |
| always | 76–95 | mysqli_report(), mysqli_init(), ->parse_db_host(), mysqli_real_connect(), ->set_charset(), ->set_sql_mode(), ->select() |
| always | 78–97 | mysqli_report(), mysqli_init(), ->parse_db_host(), mysqli_real_connect(), ->init_charset(), ->set_charset(), ->set_sql_mode(), ->select() |
!file_exists() | 123–141 | mysqli_report(), mysqli_init(), ->parse_db_host(), mysqli_real_connect(), wp_load_translations_early(), file_exists(), __(), __(), htmlspecialchars(), sprintf(), __(), __(), __(), __(), __(), sprintf(), ->bail() |
file_exists() | 128–146 | mysqli_report(), mysqli_init(), ->parse_db_host(), mysqli_real_connect(), wp_load_translations_early(), file_exists(), exit(), __(), __(), htmlspecialchars(), sprintf(), __(), __(), __(), __(), __(), sprintf(), ->bail() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 191 | 55–146 | 10 | |
| 8.5 | 191 | 55–146 | 10 | |
| 8.4 | 191 | 55–146 | 10 | 1 more instruction than PHP 8.3 |
| 8.3 | 190 | 55–141 | 10 | |
| 8.2 | 190 | 55–141 | 10 | |
| 8.1 | 190 | 55–141 | 10 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 191 | 55–142 | 10 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 8
- wp_load_translations_early()Attempts an early load of translations.
- __()Retrieves the translation of $text.
- wpdb::parse_db_host()Parses the DB_HOST setting to interpret it for mysqli_real_connect().
- wpdb::bail()Wraps errors in a nice header and footer and dies.
- wpdb::init_charset()Sets $this->charset and $this->collate.
- wpdb::set_charset()Sets the connection's character set.
- wpdb::set_sql_mode()Changes the current SQL mode, and ensures its WordPress compatibility.
- wpdb::select()Selects a database using the current or provided database connection.
Used by · 2
- wpdb::__construct()Connects to the database server and selects a database.
- wpdb::check_connection()Checks that the connection to the database is still up. If not, try to reconnect.
Source code
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’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();Changelog
Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/class-wpdb.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.