继续摘抄:PHP + MySql 输出 UTF-8 编码的页面的方法!

来源:互联网 发布:炉石数据查询 编辑:程序博客网 时间:2024/06/16 09:44

>PHP 文件 存储为 GBK 编码时:
    1. 若 PHP 文件中包含了要输出到浏览器的中文等宽字符,则必须将其转码为 UTF-8
       编码(使用 函数 iconv() 或 mb_convert_encoding());
    2. 若 PHP 文件中有要向 MySql 中插入、查询中文等宽字符,
       1>. 先执行以下SQL:
             set character_set_client = gbk;
                指明也即php程序发往数据库的SQL语句使用的是GBK编码,如insert;
             set character_set_connection = utf8;
                指明数据库收到SQL语句之后应当将其从character_set_client转码为
                utf8格式进行操作,如insert。(若没有这一句,插入的数据将变成问号)
             set character_set_results = utf8;
                指明数据库查询完毕之后应当以何种编码返回给调用端,如select。
>>PHP 文件 存储为 UTF-8 编码时
    只需要在数据库操作前执行 set names 'utf8' 即可。
重要提示:如果要CMD.EXE下使用命令行工具mysql.exe手工操作数据库的话,需要先执行
    SQL> set names gbk;
    否则,php页面插入的中文会在命令行里显示乱码,命令行插入的中文会在php页面里
    显示为乱码。
验证环境:
        Window XP, Eclipse 3.3.0
        Xampp 1.6.4 (Mysql 5.0.45, Apache 2.2, PHP 5.2.4)
my.ini 所有配置如下:(就压根没有配置过)
[WinMySQLAdmin]
Server=D:/Curriculum_Design/Eclipse/xampp/mysql/bin/mysqld-nt.exe
QueryInterval=10
php.ini 中,(就没有配置过)
default_charset 被注释掉,使用的是默认值;
extension=php_mbstring.dll 开启,但是没有它的任何配置。
TestUtf8_gbk.php
文件编码:GBK
内容如下:
*/
//header("content-type:text/html;charset=utf-8");
//mb_internal_encoding('UTF-8');
echo '';
$mysqli = new mysqli('localhost', 'root', '123456', 'test');
//$mysqli->query("set names 'utf8'");         // C1
//$mysqli->query('SET CHARACTER SET utf8');   // C2
$mysqli->query("set character_set_client = gbk");           // D1
$mysqli->query("set character_set_results = utf8");         // D2
$mysqli->query("set character_set_connection = utf8");      // D3
print_db_characterSet($mysqli);
//因为当前文件编码为GBK,而数据直接保存在PHP源代码中
//所以下面的编码测试显示为 CP936,
//想IE查看编码为 UTF-8 时正确显示,需要转码
$data = "管理员";  
$encodings = "UTF-8, GBK, ISO-8859-1";
$encoding = mb_detect_encoding($data, $encodings);
echo "";
echo "DATA : {$data}
";
echo "ENCODING : {$encoding}  
";
echo mb_convert_encoding("After GBK=>UTF-8 : $data
", "UTF-8", "GBK");
echo "";
$sqls = array();
$sqls[] = 'drop table if exists testutf8';
$sqls[] = 'create table if not exists testutf8(id nvarchar(32))';
    //.' default character set "utf8"';
$sqls[] = 'insert into testutf8 (id) values ("'.$data.'")';
$sqls[] = 'select id from testutf8';
foreach ($sqls as $i => $sql ){
    $result = $mysqli->query($sql);
    if (!$result){
        echo "ERROR : /$sqls[/$i] : ".$mysqli->error."
";
        exit();
    }
    if ($i == 1 && $result) {
        print_column_charset($mysqli, "test", "testutf8");
    }
    if ($i == 3 && $result){
        $row = $result->fetch_assoc();
        $data = $row["id"];
        $encoding = mb_detect_encoding($data, $encodings);
        echo "";
        echo "DB DATA : {$data}
";
        echo "ENCODING : {$encoding}
" ;
        echo "";
        $result->close();
    }   
}
$mysqli->close();
function print_db_characterSet($mysqli){
    //select COLUMN_NAME, CHARACTER_SET_NAME from information_schema.columns where TABLE_SCHEMA='test' and TABLE_NAME='t';
    $sql = "SHOW VARIABLES LIKE 'character_set%'";
    $result = $mysqli->query($sql);
    if(!$result){
        echo "ERROR : ".$mysqli->error."
";
        return;
    }
    echo "SQL> {$sql};
";
    echo " Variable_name"
        ." Value";
    while($row = $result->fetch_array()){
         echo "{$row[0]}$row[1]";
    }
    echo "
";
    $result->close();
    $sql = "SHOW VARIABLES LIKE 'collation%'";
    $result = $mysqli->query($sql);
    if(!$result){
        echo "ERROR : ".$mysqli->error."
";
        return;
    }
    echo "SQL> {$sql};
";
    echo " Variable_name"
        ." Value";
    while($row = $result->fetch_array()){
         echo "{$row[0]}$row[1]";
    }
    echo "
";
    $result->close();
}
function print_column_charset($mysqli, $database_name, $table_name){
    $sql = "select COLUMN_NAME, CHARACTER_SET_NAME "
            ."from information_schema.columns "
            ."where TABLE_SCHEMA='{$database_name}' and TABLE_NAME='{$table_name}'";
    $result = $mysqli->query($sql);
    if(!$result){
        //echo "ERROR : ".$mysqli->error."
";
        return;
    }
    echo "SQL> {$sql};
";
    echo " COLUMN_NAME"
        ." CHARACTER_SET_NAME";
    while($row = $result->fetch_array()){
         echo "{$row[0]}$row[1]";
    }
    echo "
";
    $result->close();
}
?>


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/25218/showart_702760.html

原创粉丝点击