MySQL::SelectDatabase PHP Method

SelectDatabase() public method

Selects a different database and character set
public SelectDatabase ( string $database, string $charset = "" ) : boolean
$database string Database name
$charset string (Optional) Character set (i.e. utf8)
return boolean Returns TRUE on success or FALSE on error
    public function SelectDatabase($database, $charset = "")
    {
        $return_value = true;
        if (!$charset) {
            $charset = $this->db_charset;
        }
        $this->ResetError();
        if (!mysqli_select_db($this->mysql_link, $database)) {
            $this->SetError();
            $return_value = false;
        } else {
            if (strlen($charset) > 0) {
                if (!mysqli_query($this->mysql_link, "SET CHARACTER SET '{$charset}'")) {
                    $this->SetError();
                    $return_value = false;
                }
            }
        }
        return $return_value;
    }

Usage Example

Ejemplo n.º 1
0
 $db = new MySQL();
 //
 // Try database connection
 //
 if (!$db->Open(null, $_SESSION['variables']['db_host'], $_SESSION['variables']['db_user'], $_SESSION['variables']['db_pass'], 'utf8', 'utf8_unicode_ci')) {
     $errors[] = 'Error: could not connect to the database engine';
     $errors[] = $db->Error();
     $errors[] = $db->MyDyingMessage();
     $err++;
 } else {
     $log[] = "Database engine connection successful";
 }
 //
 // Either Select the database or create it when it does not exist yet
 //
 if (!$db->SelectDatabase($_SESSION['variables']['db_name'])) {
     if (!$db->CreateDatabase($_SESSION['variables']['db_name'])) {
         $errors[] = 'Error: could not create the database "' . $_SESSION['variables']['db_name'] . '"';
         $errors[] = $db->Error();
         $errors[] = $db->MyDyingMessage();
         $err++;
     } else {
         $log[] = "Database creation successful";
     }
     // and once created, try to select it, again:
     if (!$db->SelectDatabase($_SESSION['variables']['db_name'])) {
         $errors[] = 'Error: could not switch to the newly created database "' . $_SESSION['variables']['db_name'] . '"';
         $errors[] = $db->Error();
         $errors[] = $db->MyDyingMessage();
         $err++;
     } else {
All Usage Examples Of MySQL::SelectDatabase