doufuxing8691 2015-03-28 20:17
浏览 157
已采纳

从MySQL获取中文字符

I am trying to display chinese characters that I have saved in MySQL database, which is saved under utf8_unicode_ci type. I have seen several solutions on the web, but nothing works.

Below is my connection file:

$conn = mysql_connect("localhost","root","password");
mysql_set_charset('utf8',$conn);
mysql_query("SET CHARACTER SET utf8 ");
mysql_select_db("database");

Below is my query:

mysql_query("SET character_set_results=utf8", $conn);

$sql = mysql_query("select * from webdata",$conn);

But it still shows ????. Any ideas?

  • 写回答

3条回答 默认 最新

  • ds3016 2016-01-05 16:33
    关注

    How to resolve...

    When I had a similar issue I firstly displayed the encoding of my text in php using

    echo mb_detect_encoding ( $text );
    

    It shows the encoding of the text coming from my query. This showed me that I was getting ASCII from mysql_query when Chinese or Russian characters were in my database.

    The change I made was with the following addition after the mysql_connect

    mysql_set_charset('UTF8');
    

    My database is utf8_unicode_ci collation and I can see chinese characters.

    So, if mb_detect_encoding is now giving you UTF8 for your text then you would be able to show chinese characters.

    The next step is to make sure what you pass to the browser has the correct header...

    header('Content-Type: text/html; charset=UTF-8');
    

    Put the above at the top of your code in php to make sure the browser is expecting your encoding.

    Now that should the question, however ideally you should be using PDO or mysqli rather than mysql_connect method. In this case the equivalent procedural style commands are..

    $link = mysqli_connect('localhost', 'my_user', 'my_password', 'test');
    mysqli_set_charset($link, "utf8");
    

    Where $link is the equivalent to your connection to the database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大