使用PHP和MySQL显示日语字符时出现问题 [英] Problems displaying Japanese characters using PHP and MySQL

查看:74
本文介绍了使用PHP和MySQL显示日语字符时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP连接到MySQL数据库来创建XML文件.除字符编码外,其他所有东西都在工作.我需要日文和英文字符,因此很明显我选择使用UTF-8.唯一的问题是数据库中的日语字符无法正确显示.

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.

数据库和表上的排序规则设置为UTF8_general_ci,MySQL连接排序规则也是如此.

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

我的php文件定义在2个不同的地方使用UTF-8(并保存在UTF-8中,而没有BOM),一次在标题中包含以下行: header("Content-type:text/xml; charset = utf-8"); 它定义它的另一个位置在XML输出文件中.

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.

作为测试,我让php文件仅从代码内编写了一些日语字符,因此它不是来自数据库.这可以正确显示(可以在此处 http://jlearn.0sites.net/Flash/xml/xml.php ...最后5个条目带有一些日语,后面加上问号,这是因为日语应来自数据库.

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.

有什么想法吗?

推荐答案

实际上只是发布了此内容- php mysql查询编码问题

Actually just posted this - php mysql query encoding problem

我趋向于解决的事情很多;

What I tend to find solves things a lot is;

mysql_query("SET NAMES 'utf8'");

在执行任何查询之前.

文档建议您使用mysql_set_charset,但是我经常看到该功能缺失.

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);
}

这篇关于使用PHP和MySQL显示日语字符时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆