MySQL::Open PHP Method

Open() public method

Connect to specified MySQL server
public Open ( string $database = null, string $server = null, string $username = null, string $password = null, string $charset = null, boolean $pcon = false ) : boolean
$database string (Optional) Database name
$server string (Optional) Host address
$username string (Optional) User name
$password string (Optional) Password
$charset string (Optional) Character set
$pcon boolean (Optional) Persistant connection
return boolean Returns TRUE on success or FALSE on error
    public function Open($database = null, $server = null, $username = null, $password = null, $charset = null, $pcon = false)
    {
        $this->ResetError();
        // Use defaults?
        if ($database !== null) {
            $this->db_dbname = $database;
        }
        if ($server !== null) {
            $this->db_host = $server;
        }
        if ($username !== null) {
            $this->db_user = $username;
        }
        if ($password !== null) {
            $this->db_pass = $password;
        }
        if ($charset !== null) {
            $this->db_charset = $charset;
        }
        if (is_bool($pcon)) {
            $this->db_pcon = $pcon;
        }
        $this->active_row = -1;
        // Open persistent or normal connection
        if ($pcon) {
            $this->mysql_link = @mysqli_pconnect('p:' . $this->db_host, $this->db_user, $this->db_pass);
        } else {
            $this->mysql_link = @mysqli_connect($this->db_host, $this->db_user, $this->db_pass);
        }
        // Connect to mysql server failed?
        if (!$this->IsConnected()) {
            $this->SetError();
            return false;
        } else {
            // Select a database (if specified)
            if (strlen($this->db_dbname) > 0) {
                if (strlen($this->db_charset) == 0) {
                    if (!$this->SelectDatabase($this->db_dbname)) {
                        return false;
                    } else {
                        return true;
                    }
                } else {
                    if (!$this->SelectDatabase($this->db_dbname, $this->db_charset)) {
                        return false;
                    } else {
                        return true;
                    }
                }
            } else {
                return true;
            }
        }
    }

Usage Example

Beispiel #1
0
function DB_Escape($String)
{
    /******************************************************************************/
    #$__args_types = Array('string');
    #-------------------------------------------------------------------------------
    $__args__ = Func_Get_Args();
    eval(FUNCTION_INIT);
    /******************************************************************************/
    $Link =& Link_Get('DB');
    #-------------------------------------------------------------------------------
    if (!Is_Object($Link)) {
        #-------------------------------------------------------------------------------
        $Config = Config();
        #-------------------------------------------------------------------------------
        $Link = new MySQL($Config['DBConnection']);
        #-------------------------------------------------------------------------------
        if (Is_Error($Link->Open())) {
            #-------------------------------------------------------------------------------
            $Link = NULL;
            #-------------------------------------------------------------------------------
            return ERROR | @Trigger_Error('[DB_Query]: невозможно соединиться с базой данных');
            #-------------------------------------------------------------------------------
        }
        #-------------------------------------------------------------------------------
        if (Is_Error($Link->SelectDB())) {
            #-------------------------------------------------------------------------------
            $Link = NULL;
            #-------------------------------------------------------------------------------
            return ERROR | @Trigger_Error('[DB_Query]: невозможно выбрать базу данных');
            #-------------------------------------------------------------------------------
        }
        #-------------------------------------------------------------------------------
    }
    #-------------------------------------------------------------------------------
    #-------------------------------------------------------------------------------
    return MySQL_Real_Escape_String($String);
    #-------------------------------------------------------------------------------
    #-------------------------------------------------------------------------------
}
All Usage Examples Of MySQL::Open