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

发布于 2024-10-10 16:00:10 字数 679 浏览 4 评论 0原文

我有一个 MySQL 数据库,其中包含一个包含 utf8_general_ci varchar 字段的 InnoDB 表。当我通过 PHP(通过 PEAR::MDB2)获取它们并尝试输出它们(通过 Smarty)时,我得到???符号。我想知道如何解决这个问题,这很可能是由 PHP 引起的。

需要了解的好信息:

  • 这是我正在开发的网站的新版本,旧版本即使没有使用 Smarty 也没有使用 MDB2,也有同样的问题,所以它们很可能不是原因。老程序员使用 htmlentities() 来解决这个问题,但我试图避免这种情况。
  • 我的所有文件(模板、源文件等)的字符编码都是 UTF-8 without BOM。
  • 当我显示页面时,所有重音字符(模板中的字符,而不是来自 MySQL 的字符)都会正确显示,并且浏览器中的编码是 UTF-8。如果我手动将其切换为 ISO-8859-1,则 MySQL 中的字符会正确输出,但其他字符不会输出。

基本上,PHP 或 MySQL 似乎在查询/获取过程中的某个时刻将数据库中包含的 UTF-8 数据转换为 ISO-8859-1,这就是我想要修复的问题。

我已经进行了大量搜索,但没有找到任何解决方案,我希望问题出在某个地方的设置上。我想避免使用 htmlentities() 或 utf8_encode(),但这可能是 PHP6 出现之前的唯一方法。

感谢您对此的意见!

I have a MySQL database with an InnoDB table containning utf8_general_ci varchar fields. When I fetch them through PHP (via PEAR::MDB2) and try to output them (via Smarty), I get ??? symbols. I would like to know how to fix that problem, which is most likely caused by 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. The old programmer used htmlentities() to remedy the problem, but I'm trying to avoid that.
  • The character encoding of all my files (template, source, etc.) is UTF-8 without 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. If I manually switch it over to ISO-8859-1, then the character from MySQL are outputed correctly, but no the others.

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.

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.

Thank you for your input on this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

祁梦 2024-10-17 16:00:10

您需要执行一些查询来告诉它使用 UTF-8 进行连接(默认值确实是 Latin-1)。这是我使用的:

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

我知道其中一些看起来有点过分,但它们已经过测试并且看起来确实工作得很好......

You need to execute a few queries to tell it to use UTF-8 for the connection (the default is indeed 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...

听风吹 2024-10-17 16:00:10

我的猜测是数据在到达数据库时不是 utf-8 编码的。

My guess is the data wasn't utf-8-encoded when it hit the database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文