dongzhazhuo0572 2011-01-19 22:21
浏览 41
已采纳

使用PHP和MySQL显示日文字符的问题

I am using PHP connecting to an MySQL database to create an XML file. Everything is working except for the character encoding. I need both Japanese and English characters so obviously I have chose to use UTF-8. Only problem is the Japanese characters from the database don't display correctly.

The collation on the database and tables is set to UTF8_general_ci, as is the MySQL connection collation.

My php file defines to use UTF-8 (and is saved in UTF-8 without BOM) in 2 different place, once in the header with the following line: header("Content-type: text/xml;charset=utf-8"); The other place it defines it is in the XML output file.

As a test I have had the php file write some Japanese characters just from within the code, so doesn't come from the database. This displays correctly (can be seen here http://jlearn.0sites.net/Flash/xml/xml.php ... the last 5 entries have some Japanese followed by question marks due to the Japanese that is meant to come from the database).

So the problem is most likely the database but everything looks correct to me.

Any ideas?

  • 写回答

2条回答 默认 最新

  • donglu3184 2011-01-19 22:23
    关注

    Actually just posted this - php mysql query encoding problem


    What I tend to find solves things a lot is;

    mysql_query("SET NAMES 'utf8'");
    

    Before any queries are performed.

    The documentation recommends that you use mysql_set_charset but I often see that function missing.

    if( function_exists('mysql_set_charset') ){
        mysql_set_charset('utf8', $db_con);
    }else{
        mysql_query("SET NAMES 'utf8'", $db_con);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效