简体   繁体   English

字符编码问题:MySQL 5.0 + PHP 5.2

[英]Character encoding issues: MySQL 5.0 + PHP 5.2

I have a MySQL database with an InnoDB table containning utf8_general_ci varchar fields. 我有一个带有InnoDB表的MySQL数据库,该表包含utf8_general_ci varchar字段。 When I fetch them through PHP (via PEAR::MDB2) and try to output them (via Smarty), I get ??? 当我通过PHP获取它们(通过PEAR :: MDB2)并尝试输出它们(通过Smarty)时,我得到了? symbols. 符号。 I would like to know how to fix that problem, which is most likely caused by PHP. 我想知道如何解决该问题,这很可能是由PHP引起的。

Good information to know: 要了解的好信息:

  • It is a new version of the site I'm working on, the old version had the same problem even though it didn't use Smarty nor MDB2, so they are most likely not the cause. 这是我正在处理的网站的新版本,尽管旧版本未使用Smarty或MDB2,但仍存在相同的问题,因此很可能不是原因。 The old programmer used htmlentities() to remedy the problem, but I'm trying to avoid that. 旧的程序员使用htmlentities()来解决此问题,但我正尝试避免这种情况。
  • The character encoding of all my files (template, source, etc.) is UTF-8 without BOM. 我所有文件(模板,源文件等)的字符编码均为UTF-8,不带BOM。
  • When I display a page, all accented characters (the ones in the templates, not the ones coming from MySQL) are shown correctly and the encoding in the browser is UTF-8. 当我显示页面时,所有带重音的字符(模板中的重音字符,而不是来自MySQL的重音字符)均正确显示,并且浏览器中的编码为UTF-8。 If I manually switch it over to ISO-8859-1, then the character from MySQL are outputed correctly, but no the others. 如果我手动将其切换到ISO-8859-1,则可以正确输出MySQL的字符,而不会输出其他字符。

Basically, it seems that PHP or MySQL transforms the UTF-8 data contained within the database to ISO-8859-1 at some point during the query/fetch process, and that is what I want to fix. 基本上,似乎PHP或MySQL在查询/获取过程中的某个时刻将数据库中包含的UTF-8数据转换为ISO-8859-1,这就是我要解决的问题。

I've done a lot of searching but haven't found any solution, and I'm hoping the problem lies in a setting somewhere. 我已经做了很多搜索,但是还没有找到任何解决方案,我希望问题出在某个地方。 I'd like to avoid having to use htmlentities() or utf8_encode(), however that might be the only way to go until PHP6 shows up. 我想避免使用htmlentities()或utf8_encode(),但是这可能是直到出现PHP6的唯一方法。

Thank you for your input on this! 感谢您对此的投入!

You need to execute a few queries to tell it to use UTF-8 for the connection (the default is indeed Latin-1). 您需要执行一些查询,以告知它使用UTF-8进行连接(默认确实是Latin-1)。 Here's what I use: 这是我使用的:

SET CHARACTER SET = "utf8";
SET character_set_database = "utf8";
SET character_set_connection = "utf8";
SET character_set_server = "utf8";

I know some of these seem overkill, but they have been tested and do seem to work quite well... 我知道其中一些似乎过于矫kill过正,但它们已经过测试,并且确实运作得很好。

我的猜测是,数据到达数据库时未经过utf-8编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM