wpdb::check_connection( bool $allow_bail = true ): bool
- Since
- 3.9.0
- Source
wp-includes/class-wpdb.php:2124
Description
If this function is unable to reconnect, it will forcibly die, or if called after the 'template_redirect' hook has been fired, return false instead.
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- Whether the connection is up. Exits if down and $allow_bail is true.
Performance profile
How much work a call to wpdb::check_connection() 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
- Light
- Scaling
- Scales with input
- Instructions
- 12–88
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 112.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What one call costs · 25 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wpdb::check_connection() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($value) | 12 | mysqli_query() |
empty($value) && !$tries | 16–17 | did_action() |
empty($value) && $tries && ->db_connect() && !error_reporting() | 20–22 | ->db_connect() |
empty($value) && !$tries | 22–23 | error_reporting(), error_reporting(), did_action() |
empty($value) && $tries && ->db_connect() && error_reporting() | 23–25 | ->db_connect(), error_reporting() |
!empty($value) && !$tries | 24–25 | mysqli_query(), did_action() |
empty($value) && $tries && $tries === false && ->db_connect() && !error_reporting() | 25 | error_reporting(), ->db_connect() |
empty($value) && $tries && ->db_connect() && !error_reporting() | 26–28 | error_reporting(), error_reporting(), ->db_connect() |
!empty($value) && $tries && ->db_connect() && !error_reporting() | 28–30 | mysqli_query(), ->db_connect() |
empty($value) && $tries && $tries === false && ->db_connect() && error_reporting() | 28 | error_reporting(), ->db_connect(), error_reporting() |
empty($value) && $tries && ->db_connect() && error_reporting() | 29–31 | error_reporting(), error_reporting(), ->db_connect(), error_reporting() |
!empty($value) && !$tries | 30–31 | mysqli_query(), error_reporting(), error_reporting(), did_action() |
13 further outcomes, up to 88 instructions
!empty($value) && $tries && ->db_connect() && error_reporting() | 31–33 | mysqli_query(), ->db_connect(), error_reporting() |
empty($value) && $tries && $tries === false && ->db_connect() && !error_reporting() | 31 | error_reporting(), error_reporting(), error_reporting(), ->db_connect() |
!empty($value) && $tries && $tries === false && ->db_connect() && !error_reporting() | 33 | mysqli_query(), error_reporting(), ->db_connect() |
!empty($value) && $tries && ->db_connect() && !error_reporting() | 34–36 | mysqli_query(), error_reporting(), error_reporting(), ->db_connect() |
empty($value) && $tries && $tries === false && ->db_connect() && error_reporting() | 34 | error_reporting(), error_reporting(), error_reporting(), ->db_connect(), error_reporting() |
!empty($value) && $tries && $tries === false && ->db_connect() && error_reporting() | 36 | mysqli_query(), error_reporting(), ->db_connect(), error_reporting() |
!empty($value) && $tries && ->db_connect() && error_reporting() | 37–39 | mysqli_query(), error_reporting(), error_reporting(), ->db_connect(), error_reporting() |
!empty($value) && $tries && $tries === false && ->db_connect() && !error_reporting() | 39 | mysqli_query(), error_reporting(), error_reporting(), error_reporting(), ->db_connect() |
!empty($value) && $tries && $tries === false && ->db_connect() && error_reporting() | 42 | mysqli_query(), error_reporting(), error_reporting(), error_reporting(), ->db_connect(), error_reporting() |
empty($value) && !$tries && !did_action() | 74 | did_action(), wp_load_translations_early(), __(), __(), htmlspecialchars(), sprintf(), __(), __(), __(), __(), sprintf(), ->bail(), dead_db() |
empty($value) && !$tries && !did_action() | 80 | error_reporting(), error_reporting(), did_action(), wp_load_translations_early(), __(), __(), htmlspecialchars(), sprintf(), __(), __(), __(), __(), sprintf(), ->bail(), dead_db() |
!empty($value) && !$tries && !did_action() | 82 | mysqli_query(), did_action(), wp_load_translations_early(), __(), __(), htmlspecialchars(), sprintf(), __(), __(), __(), __(), sprintf(), ->bail(), dead_db() |
!empty($value) && !$tries && !did_action() | 88 | mysqli_query(), error_reporting(), error_reporting(), did_action(), wp_load_translations_early(), __(), __(), htmlspecialchars(), sprintf(), __(), __(), __(), __(), sprintf(), ->bail(), dead_db() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 112 instructions, 12–88 executed per call, 10 branches. The work does not change between versions.
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 · 6
- did_action()Retrieves the number of times an action has been fired during the current request.
- wp_load_translations_early()Attempts an early load of translations.
- __()Retrieves the translation of $text.
- dead_db()Loads custom DB error or display WordPress DB error.
- wpdb::db_connect()Connects to and selects database.
- wpdb::bail()Wraps errors in a nice header and footer and dies.
Used by · 1
- wpdb::query()Performs a database query, using current database connection.
Source code
public function check_connection( $allow_bail = true ) { // Check if the connection is alive. if ( ! empty( $this->dbh ) && mysqli_query( $this->dbh, 'DO 1' ) !== false ) { return true; } $error_reporting = false; // Disable warnings, as we don't want to see a multitude of "unable to connect" messages. if ( WP_DEBUG ) { $error_reporting = error_reporting(); error_reporting( $error_reporting & ~E_WARNING ); } for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) { /* * On the last try, re-enable warnings. We want to see a single instance * of the "unable to connect" message on the bail() screen, if it appears. */ if ( $this->reconnect_retries === $tries && WP_DEBUG ) { error_reporting( $error_reporting ); } if ( $this->db_connect( false ) ) { if ( $error_reporting ) { error_reporting( $error_reporting ); } return true; } sleep( 1 ); } /* * If template_redirect has already happened, it's too late for wp_die()/dead_db(). * Let's just return and hope for the best. */ if ( did_action( 'template_redirect' ) ) { return false; } if ( ! $allow_bail ) { return false; } wp_load_translations_early(); $message = '<h1>' . __( 'Error reconnecting to the database' ) . "</h1>\n"; $message .= '<p>' . sprintf( /* translators: %s: Database host. */ __( 'This means that the contact with the database server at %s was lost. This could mean your host’s database server is down.' ), '<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>' ) . "</p>\n"; $message .= "<ul>\n"; $message .= '<li>' . __( 'Are you sure the database server is running?' ) . "</li>\n"; $message .= '<li>' . __( 'Are you sure the database server is not under particularly heavy load?' ) . "</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"; // We weren't able to reconnect, so we better bail. $this->bail( $message, 'db_connect_fail' ); /* * Call dead_db() if bail didn't die, because this database is no more. * It has ceased to be (at least temporarily). */ dead_db(); }Changelog
Introduced in 3.9.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
bool|void to bool.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.