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条)

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?