字符编码不匹配

发布于 2024-09-08 02:27:35 字数 574 浏览 4 评论 0原文

我的脚本肯定是以 UTF-8 保存的。我正在使用 "{$this->engine}:host={$this->host};dbname={$this->name};charset=UTF-8"。我的表使用 InnoDB 并使用 utf8_general_ci 进行整理。我的页面使用 Content-Type: text/html 发送; charset=UTF-8 标头或等效的 标头。

当使用 PDO 存储源自 HTTP 输入或源代码中的字符串文字的 € 字符时,根据 MySQL Workbench 5.2,我留下了 c3 a2 e2 80 9a c2 ac。从数据库中检索它并将其显示在页面上效果很好。然而在 phpMyAdmin 和 Workbench 中,我看到

当使用这两个工具存储 € 时,我留下了 e2 82 ac,这显然是正确的 UTF-8 表示形式,但如果我尝试检索然后用 PHP 输出它,� 显示。

我的问题是,这种差异从何而来?我的网页和数据库工具是否可以完美运行?

My scripts are definitely saved in UTF-8. I'm instantiating PDO with "{$this->engine}:host={$this->host};dbname={$this->name};charset=UTF-8". My tables use InnoDB and are collated using utf8_general_ci. My pages are sent either with the Content-Type: text/html; charset=UTF-8 header or the <meta> equivalent.

When using PDO to store a € character originating either from HTTP input or a string literal in the source code, I'm left with c3 a2 e2 80 9a c2 ac according to MySQL Workbench 5.2. Retrieving this from the database and displaying it on the page works fine. Yet in phpMyAdmin and Workbench, I see €.

When using these two tools to store a €, I'm left with e2 82 ac, which is apparently the correct UTF-8 representation, but if I try to retrieve and then output this with PHP, � is displayed.

My question is, from where does this discrepancy arise and is it possible to have my web pages and database tools both work flawlessly?

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

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

发布评论

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

评论(3

孤独岁月 2024-09-15 02:27:35

DSN 中的字符集指令实际上适用于 MSSQL。我只需要执行SET NAMES。对此感到抱歉。

我这样实现了:

parent::__construct("{$this->engine}:host={$this->host};dbname={$this->name}",
                    $this->user, $this->password,
                    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));

The charset directive in the DSN actually applies to MSSQL. I simply needed to execute SET NAMES. Sorry about that.

I achieved that thus:

parent::__construct("{$this->engine}:host={$this->host};dbname={$this->name}",
                    $this->user, $this->password,
                    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
柳若烟 2024-09-15 02:27:35

简单地说,将 € 转换为 html 实体 €对于超级修复(但您可能会遇到其他特殊字符的问题)

问题的另一面(更具体地说)是检查 mysql character_set_server 和最常见的原因 character_set_client - 另请参阅:连接字符集

另外,重要的是要注意元等效没有区别,您需要始终设置标头('内容类型:...)

simply, convert € to an html entity € for the superfix (but you may get the issue with other special chars)

the other side of the problem (and more specifically) is check the mysql character_set_server and the most common cause character_set_client - seeAlso: connection charsets

also, it's important to note that the meta equiv makes no difference, you need to always set the header('Content-Type: ...)

放血 2024-09-15 02:27:35

由于您的 HTML 页面是正确的,因此您似乎在数据库中存储了正确的数据。

- 是欧元符号的 UTF-8 编码,被误解为 windows-1252。您的 Windows 工具似乎使用“ANSI”编码而不是数据库编码。

Since your HTML page is correct, it seems that you're storing the correct data in the database.

€ is the UTF-8 encoding of the Euro sign misinterpreted as windows-1252. It appears that your Windows tools use "ANSI" encoding instead of the database encoding.

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