设置编码
void|boolean Driver_Database_Driver_MySQL::set_charset( string $charset )
参数列表
参数 类型 描述 默认值 $charset
string
$charset
void|boolean
public function set_charset($charset)
{
if (!$charset)return;
$connection_id = $this->connection_id();
$connection = Database_Driver_MySQL::$_connection_instance[$connection_id];
if (!$connection_id || !$connection)
{
$this->connect();
$this->set_charset($charset);
return;
}
static $_set_names = null;
if (null === $_set_names)
{
// Determine if we can use mysql_set_charset(), which is only
// available on PHP 5.2.3+ when compiled against MySQL 5.0+
$_set_names = ! function_exists('mysql_set_charset');
}
if (isset(Database_Driver_MySQL::$_current_charset[$connection_id]) && $charset==Database_Driver_MySQL::$_current_charset[$connection_id])
{
return true;
}
if (true===$_set_names)
{
// PHP is compiled against MySQL 4.x
$status = (bool)mysql_query('SET NAMES ' . $this->quote($charset), $connection);
}
else
{
// PHP is compiled against MySQL 5.x
$status = mysql_set_charset($charset, $connection);
}
if ($status === false)
{
throw new Exception('Error:' . mysql_error($connection), mysql_errno($connection));
}
# 记录当前设置的编码
Database_Driver_MySQL::$_current_charset[$connection_id] = $charset;
}