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.
我正在使用PHP连接到一个MySQL数据库来创建一个XML文件。除了字符编码之外,所有东西都在工作。我需要日语和英语字符,所以显然我选择了UTF-8。唯一的问题是数据库中的日文字符不能正确显示。
The collation on the database and tables is set to UTF8_general_ci, as is the MySQL connection collation.
数据库和表上的排序规则被设置为UTF8_general_ci, MySQL连接排序规则也是如此。
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文件定义在两个不同的地方使用UTF-8(并保存在UTF-8中,没有BOM),在头中使用如下一行:header(“Content-type: text/xml;charset= UTF-8”);它定义它的另一个地方是在XML输出文件中。
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).
作为一个测试,我已经让php文件从代码中写入一些日本字符,所以不是来自数据库。这显示正确(可以在这里看到http://jlearn.0sites.net/Flash/xml/xml.php…)最后的5个条目中有一些日文,后面是问号,因为日语应该来自数据库)。
So the problem is most likely the database but everything looks correct to me.
所以问题很可能是数据库,但我觉得一切都是正确的。
Any ideas?
什么好主意吗?
2 个解决方案
#1
12
Actually just posted this - php mysql query encoding problem
实际上刚刚发布了这个- php mysql查询编码问题。
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.
文档建议您使用mysql_set_charset,但我经常看到函数丢失。
if( function_exists('mysql_set_charset') ){
mysql_set_charset('utf8', $db_con);
}else{
mysql_query("SET NAMES 'utf8'", $db_con);
}
#2
0
If you are using objects for db access, then here's the solution that works for me
如果您正在使用对象进行db访问,那么这里有一个适合我的解决方案
class DB
{
public $Link;
private static $_instance;
public static function instance()
{
if (NULL === self::$_instance) {
self::$_instance = new DB;
}
return self::$_instance;
}
private function __construct()
{
$host = 'localhost';
$user = 'your_user_name';
$pass = 'your_password;
$db = 'db_name';
$this->Link = new mysqli($host, $user, $pass, $db);
$this->Link->set_charset('utf8');
}
}
#1
12
Actually just posted this - php mysql query encoding problem
实际上刚刚发布了这个- php mysql查询编码问题。
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.
文档建议您使用mysql_set_charset,但我经常看到函数丢失。
if( function_exists('mysql_set_charset') ){
mysql_set_charset('utf8', $db_con);
}else{
mysql_query("SET NAMES 'utf8'", $db_con);
}
#2
0
If you are using objects for db access, then here's the solution that works for me
如果您正在使用对象进行db访问,那么这里有一个适合我的解决方案
class DB
{
public $Link;
private static $_instance;
public static function instance()
{
if (NULL === self::$_instance) {
self::$_instance = new DB;
}
return self::$_instance;
}
private function __construct()
{
$host = 'localhost';
$user = 'your_user_name';
$pass = 'your_password;
$db = 'db_name';
$this->Link = new mysqli($host, $user, $pass, $db);
$this->Link->set_charset('utf8');
}
}