多语言文本、php 和 mysql 帮助

发布于 2024-10-24 14:16:46 字数 479 浏览 1 评论 0原文

我在尝试做我认为相对简单的事情时遇到了无穷无尽的问题:

我需要一个可以接受英语和其他语言混合的用户输入文本的表单,一些多字节(即日语、韩语等) ),并且由 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 技术交流群。

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

发布评论

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

评论(6

失与倦" 2024-10-31 14:16:46

以下是要检查 UTF8 模式的内容清单:

  • MySQL 表编码。你似乎已经做到了这一点。
  • MySQL 连接编码。执行SHOW STATUS LIKE 'char%',您将看到 MySQL 正在使用什么。您需要将 character_set_clientcharacter_set_connectioncharacter_set_results 设置为 utf8,可以通过执行 在您的应用程序中轻松设置在所有连接开始时设置名称“utf8”这是大多数人忘记检查的一项,IME。
  • 如果您使用它们,您的 CLI 和终端设置。在 bash 中,这意味着 LANG=(something).UTF-8
  • 您的源代码(除非您有 UTF8 常量文本,否则这通常不是问题)。
  • 页面编码。您似乎也对这一点,但是您的浏览器调试工具可以提供很大帮助。

一旦你做好了这一切,你的应用程序中所需要的就是mysql_real_escape_string()

哦,(遗憾的是)可以成功地将正确编码的 UTf8 文本存储在具有错误编码类型的列中或来自具有错误编码类型的连接。它也可以“正确”地返回。直到您修复所有非 UTF8 的位,此时它就会中断。

Here is a laundry list of things to check are in UTF8 mode:

  • MySQL table encoding. You seem to have already done this.
  • MySQL connection encoding. Do SHOW STATUS LIKE 'char%' and you will see what MySQL is using. You need character_set_client, character_set_connection and character_set_results set to utf8 which can easily set in your application by doing SET NAMES 'utf8' at the start of all connections. This is the one most people forget to check, IME.
  • If you use them, your CLI and terminal settings. In bash, this means LANG=(something).UTF-8.
  • Your source code (this is not usually a problem unless you have UTF8 constant text).
  • The page encoding. You seem to have this one right, too, but your browsers debug tools can help a lot.

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.

二手情话 2024-10-31 14:16:46

我认为你没有任何实用的 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.

倾听心声的旋律 2024-10-31 14:16:46

首先,您需要检查是否可以直接将多语言文本添加到数据库中。如果可能的话,您可以在您的应用程序中执行此操作

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

输什么也不输骨气 2024-10-31 14:16:46

您是否偶然序列化了任何数据? 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.

情何以堪。 2024-10-31 14:16:46

问题是由于我没有在 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!!

小草泠泠 2024-10-31 14:16:46

检查您的数据库连接设置。它还需要支持UTF-8。

Check your database connection settings. It also needs to support UTF-8.

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