Get- and SetCharSet for mysql and mysqli

This commit is contained in:
Ralf Becker 2005-11-07 13:37:21 +00:00
parent ae9ae56e30
commit 40b35348e0
2 changed files with 58 additions and 9 deletions

View File

@ -578,7 +578,7 @@ class ADODB_mysql extends ADOConnection {
return $foreign_keys; return $foreign_keys;
} }
/** /**
* @var array $charset2mysql translate www charsets to mysql ones * @var array $charset2mysql translate www charsets to mysql ones
*/ */
var $charset2mysql = array( var $charset2mysql = array(
@ -595,6 +595,8 @@ class ADODB_mysql extends ADOConnection {
/** /**
* gets the client encoding from the connection * gets the client encoding from the connection
* *
* mysqli_client_encoding only returns the default charset, not the one currently used!
*
* @return string/boolean charset or false * @return string/boolean charset or false
*/ */
function GetCharSet() function GetCharSet()
@ -617,14 +619,12 @@ class ADODB_mysql extends ADOConnection {
*/ */
function SetCharSet($charset_name) function SetCharSet($charset_name)
{ {
$this->GetCharSet(); $mysql_charset = isset($this->charset2mysql[$charset_name]) ? $this->charset2mysql[$charset_name] : $charset_name;
if ($this->charSet !== $charset_name) { if (!mysql_query('SET NAMES '.$this->qstr($mysql_charset),$this->_connectionID)) return false;
$ok = mysql_query('SET NAMES '.$this->qstr(isset($this->charset2mysql[$charset_name]) ? if ($this->GetCharSet()) {
$this->charset2mysql[$charset_name] : $charset_name)); return $this->charSet == $charset_name || $this->charset2mysql[$this->charSet] == $charset_name;
if ($ok && $this->GetCharSet() == $charset_name || $this->charset2mysql[$this->charSet] == $charset_name) { }
return true; return false;
} else return false;
} else return true;
} }
} }

View File

@ -654,7 +654,56 @@ class ADODB_mysqli extends ADOConnection {
return 4294967295; return 4294967295;
} }
/**
* @var array $charset2mysql translate www charsets to mysql ones
*/
var $charset2mysql = array(
'utf-8' => 'utf8',
'iso-8859-1' => 'latin1',
'iso-8859-2' => 'latin2',
'windows-1251' => 'cp1251',
'koi8-r' => 'koi8r', // 4.0: koi8_ru
'euc-kr' => 'euckr', // 4.0: euc_kr
'euc-jp' => 'ujis', // 4.0: -
'iso-8859-7' => 'greek', // 4.0: -
);
/**
* gets the client encoding from the connection
*
* mysqli_client_encoding only returns the default charset, not the one currently used!
*
* @return string/boolean charset or false
*/
function GetCharSet()
{
$this->charSet = $this->GetOne('SELECT @@character_set_connection');
if ($this->charSet) {
$mysql2charset = array_flip($this->charset2mysql);
if (isset($mysql2charset[$this->charSet])) {
$this->charSet = $mysql2charset[$this->charSet];
}
}
return $this->charSet ? $this->charSet : false;
}
/**
* sets the client encoding from the connection
*
* mysqli_set_charset is php5.1+, the query used here works since mysql4.1
*
* @param string $charset_name
* @return boolean true on success, false otherwise
*/
function SetCharSet($charset_name)
{
$mysql_charset = isset($this->charset2mysql[$charset_name]) ? $this->charset2mysql[$charset_name] : $charset_name;
if (!mysqli_query($this->_connectionID,'SET NAMES '.$this->qstr($mysql_charset))) return false;
if ($this->GetCharSet()) {
return $this->charSet == $charset_name || $this->charset2mysql[$this->charSet] == $charset_name;
}
return false;
}
} }
/*-------------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------------