wpdb::check_connection PHP Method

check_connection() public method

If this function is unable to reconnect, it will forcibly die, or if after the 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.
Since: 3.9.0
public check_connection ( boolean $allow_bail = true ) : boolean
$allow_bail boolean Optional. Allows the function to bail. Default true.
return boolean True if the connection is up.
    public function check_connection($allow_bail = true)
    {
        if ($this->use_mysqli) {
            if (@mysqli_ping($this->dbh)) {
                return true;
            }
        } else {
            if (@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;
        }
        // We weren't able to reconnect, so we better bail.
        $this->bail(sprintf("\n<h1>Error reconnecting to the database</h1>\n<p>This means that we lost contact with the database server at <code>%s</code>. This could mean your host's database server is down.</p>\n<ul>\n\t<li>Are you sure that the database server is running?</li>\n\t<li>Are you sure that the database server is not under particularly heavy load?</li>\n</ul>\n<p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='https://wordpress.org/support/'>WordPress Support Forums</a>.</p>\n", htmlspecialchars($this->dbhost, ENT_QUOTES)), '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();
    }

Usage Example

    /**
     * Display Settings page of the module.
     */
    public function data_collector_page()
    {
        $dc_hostname = get_option('ltg_q_dc_hostname', '');
        $dc_port = get_option('ltg_q_dc_port', '');
        $dc_database = get_option('ltg_q_dc_database', '');
        $dc_login = get_option('ltg_q_dc_login', '');
        $dc_password = get_option('ltg_q_dc_password', '');
        // Connect to the data collector server
        $dc_con = new wpdb($dc_login, $dc_password, $dc_database, $dc_hostname . ':' . $dc_port);
        // Check connection to the server
        if (!$dc_con->check_connection(false)) {
            echo '<div class="wrap">
  <h2>Data Collector</h2>
  <p>The connection to the database cannot be established. Please update the
  <a href="admin.php?page=ltg-questionnaires">settings of the Data Collector</a>.</p>
</div>';
            exit;
        }
        include_once dirname(__FILE__) . '/partials/ltg_questionnaires-data-collector.php';
    }
All Usage Examples Of wpdb::check_connection