wpdb::db_connect PHP Method

db_connect() public method

If $allow_bail is false, the lack of database connection will need to be handled manually.
Since: 3.0.0
Since: 3.9.0 $allow_bail parameter added.
public db_connect ( boolean $allow_bail = true ) : boolean
$allow_bail boolean Optional. Allows the function to bail. Default true.
return boolean True with a successful connection, false on failure.
    public function db_connect($allow_bail = true)
    {
        $this->is_mysql = true;
        /*
         * Deprecated in 3.9+ when using MySQLi. No equivalent
         * $new_link parameter exists for mysqli_* functions.
         */
        $new_link = defined('MYSQL_NEW_LINK') ? MYSQL_NEW_LINK : true;
        $client_flags = defined('MYSQL_CLIENT_FLAGS') ? MYSQL_CLIENT_FLAGS : 0;
        if ($this->use_mysqli) {
            $this->dbh = mysqli_init();
            // mysqli_real_connect doesn't support the host param including a port or socket
            // like mysql_connect does. This duplicates how mysql_connect detects a port and/or socket file.
            $port = null;
            $socket = null;
            $host = $this->dbhost;
            $port_or_socket = strstr($host, ':');
            if (!empty($port_or_socket)) {
                $host = substr($host, 0, strpos($host, ':'));
                $port_or_socket = substr($port_or_socket, 1);
                if (0 !== strpos($port_or_socket, '/')) {
                    $port = intval($port_or_socket);
                    $maybe_socket = strstr($port_or_socket, ':');
                    if (!empty($maybe_socket)) {
                        $socket = substr($maybe_socket, 1);
                    }
                } else {
                    $socket = $port_or_socket;
                }
            }
            if (WP_DEBUG) {
                mysqli_real_connect($this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags);
            } else {
                @mysqli_real_connect($this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags);
            }
            if ($this->dbh->connect_errno) {
                $this->dbh = null;
                /* It's possible ext/mysqli is misconfigured. Fall back to ext/mysql if:
                 *  - We haven't previously connected, and
                 *  - WP_USE_EXT_MYSQL isn't set to false, and
                 *  - ext/mysql is loaded.
                 */
                $attempt_fallback = true;
                if ($this->has_connected) {
                    $attempt_fallback = false;
                } else {
                    if (defined('WP_USE_EXT_MYSQL') && !WP_USE_EXT_MYSQL) {
                        $attempt_fallback = false;
                    } else {
                        if (!function_exists('mysql_connect')) {
                            $attempt_fallback = false;
                        }
                    }
                }
                if ($attempt_fallback) {
                    $this->use_mysqli = false;
                    $this->db_connect();
                }
            }
        } else {
            if (WP_DEBUG) {
                $this->dbh = mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags);
            } else {
                $this->dbh = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags);
            }
        }
        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;
            }
            $this->bail(sprintf(__("\n<h1>Error establishing a database connection</h1>\n<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact 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 you have the correct username and password?</li>\n\t<li>Are you sure that you have typed the correct hostname?</li>\n\t<li>Are you sure that the database server is running?</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');
            return false;
        } else {
            if ($this->dbh) {
                $this->has_connected = true;
                $this->set_charset($this->dbh);
                $this->set_sql_mode();
                $this->ready = true;
                $this->select($this->dbname, $this->dbh);
                return true;
            }
        }
        return false;
    }

Usage Example

コード例 #1
0
ファイル: wpdbutils.php プロジェクト: jimlongo56/bhouse
 /**
  *	kick()
  *	
  *	Kicks the database to see if the conenction is still alive and if it isn't then tries to reconnect
  *	
  *	@return		true if connection alive (may have been reconnected), false otherwise (dead and couldn't be reconnected)
  *
  */
 public function kick()
 {
     // Initialize result to assume failure
     $result = false;
     // Use ping to check if server is still present - note will not reconnect automatically for MySQL >= 5.0.13
     // and actually we don't want it to as that is bad karma
     if (!mysql_ping($this->_db->dbh)) {
         // Database connection appears to have gone away
         pb_backupbuddy::status(self::STATUS_TYPE_DETAILS, __('Database Server has gone away, attempting to reconnect.', 'it-l10n-backupbuddy'));
         // Close things down cleanly (from a local perspective)
         @mysql_close($this->_db->dbh);
         unset($this->_db->dbh);
         $this->_db->ready = false;
         // And attempt to reconnect
         $this->_db->db_connect();
         // Reconnect failed if we have a null resource or ping fails
         if (NULL == $this->_db->dbh || !mysql_ping($this->_db->dbh)) {
             // Reconnection failed, make sure user knows
             pb_backupbuddy::status(self::STATUS_TYPE_DETAILS, __('Database Server reconnection failed.', 'it-l10n-backupbuddy'));
             // Make sure failure is notified (no need to close things down locally as it's a wrap anyway)
             $result = false;
         } else {
             // Reconnection successful, make sure user knows
             pb_backupbuddy::status(self::STATUS_TYPE_DETAILS, __('Database Server reconnection successful.', 'it-l10n-backupbuddy'));
             $result = true;
         }
     } else {
         // Just to let user know that database is still connected
         pb_backupbuddy::status(self::STATUS_TYPE_DETAILS, __('Database Server connection status verified.', 'it-l10n-backupbuddy'));
         $result = true;
     }
     return $result;
 }
All Usage Examples Of wpdb::db_connect