wppaste
WordPress

wpdb::check_connection( bool $allow_bail = true ): bool|void

Since
3.9.0
Source
wp-includes/class-wpdb.php:2128
Checks that the connection to the database is still up. If not, try to reconnect.

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|void
True if the connection is up.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
12–88

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 112.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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.

WhenInstructionsCalls it makes
!empty($value)12mysqli_query()
empty($value) && !$tries16–17did_action()
empty($value) && $tries && ->db_connect() && !error_reporting()20–22->db_connect()
empty($value) && !$tries22–23error_reporting(), error_reporting(), did_action()
empty($value) && $tries && ->db_connect() && error_reporting()23–25->db_connect(), error_reporting()
!empty($value) && !$tries24–25mysqli_query(), did_action()
empty($value) && $tries && $tries === false && ->db_connect() && !error_reporting()25error_reporting(), ->db_connect()
empty($value) && $tries && ->db_connect() && !error_reporting()26–28error_reporting(), error_reporting(), ->db_connect()
!empty($value) && $tries && ->db_connect() && !error_reporting()28–30mysqli_query(), ->db_connect()
empty($value) && $tries && $tries === false && ->db_connect() && error_reporting()28error_reporting(), ->db_connect(), error_reporting()
empty($value) && $tries && ->db_connect() && error_reporting()29–31error_reporting(), error_reporting(), ->db_connect(), error_reporting()
!empty($value) && !$tries30–31mysqli_query(), error_reporting(), error_reporting(), did_action()
13 further outcomes, up to 88 instructions
!empty($value) && $tries && ->db_connect() && error_reporting()31–33mysqli_query(), ->db_connect(), error_reporting()
empty($value) && $tries && $tries === false && ->db_connect() && !error_reporting()31error_reporting(), error_reporting(), error_reporting(), ->db_connect()
!empty($value) && $tries && $tries === false && ->db_connect() && !error_reporting()33mysqli_query(), error_reporting(), ->db_connect()
!empty($value) && $tries && ->db_connect() && !error_reporting()34–36mysqli_query(), error_reporting(), error_reporting(), ->db_connect()
empty($value) && $tries && $tries === false && ->db_connect() && error_reporting()34error_reporting(), error_reporting(), error_reporting(), ->db_connect(), error_reporting()
!empty($value) && $tries && $tries === false && ->db_connect() && error_reporting()36mysqli_query(), error_reporting(), ->db_connect(), error_reporting()
!empty($value) && $tries && ->db_connect() && error_reporting()37–39mysqli_query(), error_reporting(), error_reporting(), ->db_connect(), error_reporting()
!empty($value) && $tries && $tries === false && ->db_connect() && !error_reporting()39mysqli_query(), error_reporting(), error_reporting(), error_reporting(), ->db_connect()
!empty($value) && $tries && $tries === false && ->db_connect() && error_reporting()42mysqli_query(), error_reporting(), error_reporting(), error_reporting(), ->db_connect(), error_reporting()
empty($value) && !$tries && !did_action()74did_action(), wp_load_translations_early(), __(), __(), htmlspecialchars(), sprintf(), __(), __(), __(), __(), sprintf(), ->bail(), dead_db()
empty($value) && !$tries && !did_action()80error_reporting(), error_reporting(), did_action(), wp_load_translations_early(), __(), __(), htmlspecialchars(), sprintf(), __(), __(), __(), __(), sprintf(), ->bail(), dead_db()
!empty($value) && !$tries && !did_action()82mysqli_query(), did_action(), wp_load_translations_early(), __(), __(), htmlspecialchars(), sprintf(), __(), __(), __(), __(), sprintf(), ->bail(), dead_db()
!empty($value) && !$tries && !did_action()88mysqli_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

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&#8217;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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.1.0
Return type changed from bool|void to bool.verified against source
3.9.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 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.