请教问题:
mysql是utf-8形式,我在编写网页代码时用的是gb2312,请问如何向数据库写入中文和向页面输出中文的时候,是正常的,而不会出现乱码。

解决方案 »

  1.   

    建议你前台和后台保持编码一致
    数据库写入中文 mysql_query("set names utf8");
    向页面输出中文 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />你这个地方可以使用iconv("UTF-8","GB2312",$str); 转换一下 
      

  2.   

    mysql_query("set names 'utf8'");
      

  3.   

    upmb_convert_encoding也可以编码转换
      

  4.   

    其實按照道理來說 是不會亂碼的...因為utf-8的範圍大過gb2312但是很遺憾mysql沒有utf-8
    只有utf8...兩者是有差別的...
      

  5.   

    这是向数据库插入数据的代码:
    <?php
    $user=htmlspecialchars($_POST['user']);
    $txt=htmlspecialchars($_POST['txt']);
    $mail=htmlspecialchars($_POST['mail']);
    $time=date(Y年m月d日H:i:s);
    require '../root_conn_config/mysql_conn.php';
    $sql="insert into liuyan_table(id,user,txt,mail,time) values(NULL,'".$user."','".$txt."','".$mail."','".$time."')";
    if(mysql_query($sql)){
    header("content-type:text/html;charset=utf-8");
    echo "<script>window.alert('留言成功。');window.location.href='index.php';</script>";
    }else{
    header("content-type:text/html;charset=utf-8");
    echo "<script>window.alert('留言失败,请返回检查');window.location.href='index.php';</script>";
    }
    ?>
    有朋友建议使用foreach ($arr as $k=>$v) {
            $arr["$k"] = iconv('GB2312', 'UTF-8', $v);
    }进行转码;可惜我的领悟能力有限,请朋友们给一个比较详细的代码,展示一下。
      

  6.   


    set names gbk;不需要改动客户端