多语言文本、php 和 mysql 帮助
我在尝试做我认为相对简单的事情时遇到了无穷无尽的问题:
我需要一个可以接受英语和其他语言混合的用户输入文本的表单,一些多字节(即日语、韩语等) ),并且由 php 处理并存储(安全地,避免 SQL 注入)在 mysql 数据库中。它还需要从数据库访问、处理并在屏幕上使用。
我已经为拉丁字符设置得很好,但是当我添加拉丁字符和多字节字符的混合时,它会变成乱码。
我曾尝试做作业,但现在只能用头撞墙。
魔术引号关闭,我尝试使用 utf8_encode/decode、htmlentities、addslashes/stripslashes 以及(在 mysql 中)“utf8_general_ci”和“utf8_unicode_ci”作为表中的字段。
部分问题在于,有太多地方可能会搞砸,以至于我不知道从哪里开始解决问题。
非常感谢您对此提供的所有帮助。理想情况下,如果有人有可用的 php 代码示例和/或知道正确的 mysql 表格式,那就太棒了。
I have had no end of problems trying to do what I thought would be relatively simple:
I need to have a form which can accept user input text in a mix of English an other languages, some multi-byte (ie Japanese, Korean, etc), and this gets processed by php and is stored (safely, avoiding SQL injection) in a mysql database. It also needs to be accessed from the database, processed, and used on-screen.
I have it set up fine for Latin chars but when I add a mix of Latin andmulti-byte chars it turns garbled.
I have tried to do my homework but just am banging my head against a wall now.
Magic quotes is off, I have tried using utf8_encode/decode, htmlentities, addslashes/stripslashes, and (in mysql) both "utf8_general_ci" and "utf8_unicode_ci" for the field in the table.
Part of the problem is that there are so many places where I could be messing it up that I'm not sure where to begin solving the problem.
Thanks very much for any and all help with this. Ideally, if someone has working php code examples and/or knows the right mysql table format, that would be fantastic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
以下是要检查 UTF8 模式的内容清单:
SHOW STATUS LIKE 'char%'
,您将看到 MySQL 正在使用什么。您需要将character_set_client
、character_set_connection
和character_set_results
设置为utf8
,可以通过执行在您的应用程序中轻松设置在所有连接开始时设置名称“utf8”
。 这是大多数人忘记检查的一项,IME。LANG=(something).UTF-8
。一旦你做好了这一切,你的应用程序中所需要的就是
mysql_real_escape_string()
。哦,(遗憾的是)可以成功地将正确编码的 UTf8 文本存储在具有错误编码类型的列中或来自具有错误编码类型的连接。它也可以“正确”地返回。直到您修复所有非 UTF8 的位,此时它就会中断。
Here is a laundry list of things to check are in UTF8 mode:
SHOW STATUS LIKE 'char%'
and you will see what MySQL is using. You needcharacter_set_client
,character_set_connection
andcharacter_set_results
set toutf8
which can easily set in your application by doingSET NAMES 'utf8'
at the start of all connections. This is the one most people forget to check, IME.LANG=(something).UTF-8
.Once you get all this right, all you will need in your app is
mysql_real_escape_string()
.Oh and it is (sadly) possible to successfully store correctly encoded UTf8 text in a column with the wrong encoding type or from a connection with the wrong encoding type. And it can come back "correctly", too. Until you fix all the bits that aren't UTF8, at which point it breaks.
我认为你没有任何实用的 UTF-8 替代方案。您将必须找出编码和/或解码中断的位置。首先检查是否可以从 mysql 命令行或者通过 phpmyadmin 将多语言文本往返到数据库。追踪并消除该级别的问题。然后通过模拟 php 的输入并检查输出,再移出一层,再次处理任何问题。最后添加浏览器。
I don't think you have any practical alternatives to UTF-8. You're going to have to track down where the encoding and/or decoding breaks. Start by checking whether you can round-trip multi-language text to the data base from the mysql command line, or perhaps through phpmyadmin. Track down and eliminate problems at that level. Then move out one more level by simulating input to your php and examining the output, again dealing with any problems. Finally add browsers into the mix.
首先,您需要检查是否可以直接将多语言文本添加到数据库中。如果可能的话,您可以在您的应用程序中执行此操作
First you need to check if you can add multi-language text to your database directly. If its possible you can do it in your application
您是否偶然序列化了任何数据? PHP 的序列化函数在序列化非英文字符时存在一些问题。
你所做的一切都应该是 utf-8 编码。
您可以尝试的一件事是 json_encode() 数据将其放入数据库并在检索时使用json_decoding() 。
Are you serializing any data by chance? PHPs serialize function has some issue when serializing non-english characters.
Everything you do should be utf-8 encoded.
One thing you could try is to json_encode() the data when putting it into the database and json_decoding() it when it's retrieved.
问题是由于我没有在 php.ini 文件中设置默认字符集,并且(可能)没有在 mysql 表中设置字符集(在 PhpMyAdmin 中,通过“操作”选项卡)。
将默认字符集设置为“utf-8”修复了它。感谢您的帮助!
The problem was caused by my not having the default char set in the php.ini file, and (possibly) not having set the char set in the mysql table (in PhpMyAdmin, via the Operations tab).
Setting the default char set to "utf-8" fixed it. Thanks for the help!!
检查您的数据库连接设置。它还需要支持UTF-8。
Check your database connection settings. It also needs to support UTF-8.