字符编码不匹配
我的脚本肯定是以 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DSN 中的字符集指令实际上适用于 MSSQL。我只需要执行
SET NAMES
。对此感到抱歉。我这样实现了:
The charset directive in the DSN actually applies to MSSQL. I simply needed to execute
SET NAMES
. Sorry about that.I achieved that thus:
简单地说,将 € 转换为 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: ...)
由于您的 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.