Mysql 문자셋변환
05 26, 2007 10:57
If you don't have iconv, you can use MySQL to do the conversion.
Note that to do this you have to use MySQL's charset names, which are different than iconv names (e.g. "utf8" instead of "utf-8").
<?php
/**
* @param $string
* @param $from [string] The name of the current character set of $string, in MySQL format (e.g. "utf8", "hebrew").
* @param $to [string] The name of the character set to which you want to convert $string.
* @author Erel Segal - Rent a Brain (http://tora.us.fm/rentabrain)
* @date 2006-12-10
*/
function mysql_iconv($string, $from, $to) {
// keep current character set values:
$character_set_database = mysql_result(mysql_query("SELECT @@character_set_client"),0,0);
$character_set_results = mysql_result(mysql_query("SELECT @@character_set_results"),0,0);
mysql_query("SET character_set_client=$from");
mysql_query("SET character_set_results=$to");
$string_escaped = mysql_real_escape_string($string);
$converted_string = mysql_result(mysql_query("SELECT '$string_escaped'"),0,0);
// restore previous character set values:
mysql_query("SET character_set_client=$character_set_database");
mysql_query("SET character_set_results=$character_set_results");
return $converted_string;
}
?>
Note that to do this you have to use MySQL's charset names, which are different than iconv names (e.g. "utf8" instead of "utf-8").
<?php
/**
* @param $string
* @param $from [string] The name of the current character set of $string, in MySQL format (e.g. "utf8", "hebrew").
* @param $to [string] The name of the character set to which you want to convert $string.
* @author Erel Segal - Rent a Brain (http://tora.us.fm/rentabrain)
* @date 2006-12-10
*/
function mysql_iconv($string, $from, $to) {
// keep current character set values:
$character_set_database = mysql_result(mysql_query("SELECT @@character_set_client"),0,0);
$character_set_results = mysql_result(mysql_query("SELECT @@character_set_results"),0,0);
mysql_query("SET character_set_client=$from");
mysql_query("SET character_set_results=$to");
$string_escaped = mysql_real_escape_string($string);
$converted_string = mysql_result(mysql_query("SELECT '$string_escaped'"),0,0);
// restore previous character set values:
mysql_query("SET character_set_client=$character_set_database");
mysql_query("SET character_set_results=$character_set_results");
return $converted_string;
}
?>
Trackback Address:http://limcom.co.kr/blog/trackback/88