wpdb::check_connection() WordPress Method

The wpdb::check_connection() method is used to check if a database connection is still active. If the connection is no longer active, this method will try to reconnect.

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

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.


Top ↑

Parameters

$allow_bail

(bool)(Optional) Allows the function to bail.

Default value: true


Top ↑

Return

(bool|void) True if the connection is up.


Top ↑

Source

File: wp-includes/wp-db.php

1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
public function check_connection( $allow_bail = true ) {
    if ( $this->use_mysqli ) {
        if ( ! empty( $this->dbh ) && mysqli_ping( $this->dbh ) ) {
            return true;
        }
    } else {
        if ( ! empty( $this->dbh ) && mysql_ping( $this->dbh ) ) {
            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>.' ),
    ) . "</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();
}


Top ↑

Changelog

Changelog
VersionDescription
3.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.