使用 utf8_encode()(php 函数)的国际化内容最佳实践
为了让网站接受用户提交的可能不是英语(例如日语)的内容并将其保存到数据库中,对所有新内容进行 utf8_encode 并在稍后检索时使用 utf8_decode 是否符合我的最佳利益?
更多信息: 我正在使用原则,并且在尝试将 Unicode 字符保存或选择到 MySQL 数据库时遇到错误:
SQLSTATE[HY000]:一般错误:1267 操作“=”的排序规则 (latin1_swedish_ci,IMPLICIT) 和 (utf8_general_ci,COERCIBLE) 的非法混合
In order for website to accept user submitted content which may not be in English (e.g. Japanese) and save it to the database, is it in my best interest to utf8_encode all new content, and user utf8_decode when retrieving it later?
Further info:
I am using doctrine and I am getting an errors when attempting to save or select Unicode characters to the MySQL database:
SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要使用编码功能。您需要做的是确保端到端都是 UTF8。看起来您的数据库可能正在使用 latin1 编码和排序规则。您与数据库的连接也需要是 UTF8。有时,只需在建立连接后立即执行 SET NAMES UTF8 查询即可。
在 mysql 中运行此命令可能会解决您在上面看到的错误,但您仍然需要端到端 UTF8。那么您无需对数据执行任何特殊操作。
You don't need to use the encode function. What you need to do is make sure you are UTF8 end to end. Looks like you database might be using latin1 encoding and collation. Your connection to the database also needs to be UTF8. Sometimes that simply a matter of executing SET NAMES UTF8 query right after you establish a connection.
Running this command in mysql will likely resolve the error you see above, but you still need to be end-to-end UTF8. Then you don't need to do anything special with your data.
布伦特是对的。它需要是端到端的。这是我的清单:
Brent is right. It needs to be end-to-end. Here's my list: