Mysql SET NAMES UTF8 - 如何摆脱?

问题描述:

在一个非常繁忙的PHP脚本中,我们在开头调用设置名称utf8,这是设置mysql应解释的字符集,并将数据从服务器发回客户端。

In a very busy PHP script we have a call at the beginning to "Set names utf8" which is setting the character set in which mysql should interpret and send the data back from the server to the client.

http:// dev.mysql.com/doc/refman/5.0/en/charset-applications.html

我想摆脱它,所以我设置default- character-set = utf8在我们的服务器ini文件中。 (参见上面的链接)

I want to get rid of it so I set default-character-set=utf8 In our server ini file. (see link above)

设置似乎工作,因为相关的服务器参数是:
'character_set_client','utf8'
'character_set_connection ','utf8'
'character_set_database','latin1'
'character_set_filesystem','binary'
'character_set_results','utf8'
'character_set_server','latin1'
'character_set_system','utf8'
但是在此更改和注释掉名称后utf8调用仍然出现数据开始出现乱码。

The setting seems to be working since the relevant server parameters are : 'character_set_client', 'utf8' 'character_set_connection', 'utf8' 'character_set_database', 'latin1' 'character_set_filesystem', 'binary' 'character_set_results', 'utf8' 'character_set_server', 'latin1' 'character_set_system', 'utf8' But after this change and commenting out set names utf8 call still the data starts to come out garbled.

建议....

设置PHP和MySQL之间的连接的编码是PHP的工作;我不认为MySQL的设置会影响。

Setting the encoding of the connection between PHP and MySQL is PHP's job; I don't think the MySQL setting will affect that.

我真的建议保持一些代码在应用程序中设置连接字符集为UTF-8。应用程序需要确保编码是UTF-8,因为它可能是告诉Web浏览器其页面是UTF-8,如果这些不匹配,你有问题。

I'd really recommend keeping some code in the application to set the connection character set to UTF-8. The application needs to ensure that the encoding is UTF-8, because it will presumably be telling web browsers its pages are UTF-8, and if those don't match you've got problems.

由于它已经是应用程序负责决定字符集的一部分,因此您可以使应用程序成为指定数据库连接字符集的应用程序,而不是将其作为一个部署问题,还有一件事要获得在新服务器上安装应用程序时出错。

Since it's already part of the application's responsibility to decide the charset, you might as well make the application the one to specify the database connection character set, rather than leave it as a deployment issue and one more thing to get wrong when installing the app on a new server.

但我个人会使用 mysql_set_charset ,而不是 SET NAMES

However I would personally use mysql_set_charset to do this rather than SET NAMES.

同样,如果应用程序中有代码来创建模式,请确保代码告诉MySQL使表UTF-8,而不是留给数据库的默认设置,给自己另一个部署问题担心。

Similarly, if the application has code in it to create the schema, make sure that code tells MySQL to make the tables UTF-8, rather than leaving it up to the database's default settings and giving yourself another deployment issue to worry about.