DBConnection::establishDBConnection PHP Method

establishDBConnection() static public method

Establish a connection to a mysql server (main or replicate)
static public establishDBConnection ( $use_slave, $required, $display = true )
$use_slave try to connect to slave server first not to main server
$required connection to the specified server is required (if connection failed, do not try to connect to the other server)
$display display error message (true by default)
    static function establishDBConnection($use_slave, $required, $display = true)
    {
        global $DB;
        $DB = null;
        $res = false;
        // First standard config : no use slave : try to connect to master
        if (!$use_slave) {
            $res = self::switchToMaster();
        }
        // If not already connected to master due to config or error
        if (!$res) {
            // No DB slave : first connection to master give error
            if (!self::isDBSlaveActive()) {
                // Slave wanted but not defined -> use master
                // Ignore $required when no slave configured
                if ($use_slave) {
                    $res = self::switchToMaster();
                }
            } else {
                // Slave DB configured
                // Try to connect to slave if wanted
                if ($use_slave) {
                    $res = self::switchToSlave();
                }
                // No connection to 'mandatory' server
                if (!$res && !$required) {
                    //Try to establish the connection to the other mysql server
                    if ($use_slave) {
                        $res = self::switchToMaster();
                    } else {
                        $res = self::switchToSlave();
                    }
                    if ($res) {
                        $DB->first_connection = false;
                    }
                }
            }
        }
        // Display error if needed
        if (!$res && $display) {
            self::displayMySQLError();
        }
        return $res;
    }

Usage Example

    echo "GLPI_DB_OK\n";
} else {
    echo "GLPI_DB_PROBLEM\n";
    $ok_master = false;
}
// Slave and master ok;
$ok = $ok_slave && $ok_master;
// Check session dir (usefull when NFS mounted))
if (is_dir(GLPI_SESSION_DIR) && is_writable(GLPI_SESSION_DIR)) {
    echo "GLPI_SESSION_DIR_OK\n";
} else {
    echo "GLPI_SESSION_DIR_PROBLEM\n";
    $ok = false;
}
// Reestablished DB connection
if (($ok_master || $ok_slave) && DBConnection::establishDBConnection(false, false, false)) {
    // Check LDAP Auth connections
    $ldap_methods = getAllDatasFromTable('glpi_authldaps', '`is_active`=1');
    if (count($ldap_methods)) {
        echo "Check LDAP servers:";
        foreach ($ldap_methods as $method) {
            echo " " . $method['name'];
            if (AuthLDAP::tryToConnectToServer($method, $method["rootdn"], Toolbox::decrypt($method["rootdn_passwd"], GLPIKEY))) {
                echo "_OK";
            } else {
                echo "_PROBLEM";
                $ok = false;
            }
            echo "\n";
        }
    } else {
All Usage Examples Of DBConnection::establishDBConnection