双UTF-8编码,但为什么呢? PHP/MySQL

发布于 2024-11-04 11:28:35 字数 839 浏览 2 评论 0原文

我有一些表单,将一些数据插入 MySQL 数据库,并且由于某种原因,字符得到双 utf-8 编码。你在我网站的前端看不到它,但在后端你会看到它,如果我查看 phpmyadmin 的数据,它是双重编码的。

另外,要显示从 phpmyadmin 输入的数据,我必须对其进行 utf8_encode

如果我在将数据放入数据库之前对其使用 uft8_decode() ,它会起作用,但随后我必须再次使用 utf8_encode() 来显示我的数据正确地,我想找到一个更好的解决方案来重写我的大部分代码。

我正在处理的字符是丹麦语 æ、ø 和 å 字符。

我在 php.ini 中找到的所有设置都设置为 utf-8,在 phpmyadmin 中找到的所有设置都设置为 utf-8,html 元标记设置为 utf-8,但我仍然有这个错误需要处理。

所以我的问题是,有谁知道为什么会发生这种情况,或者我该如何解决它?..

更新:运行 Jako 建议的 mysql 代码后,数据在后端正确编码当它来自前端时数据库,但我仍然需要运行 utf8_encode() 才能在前端正确显示数据,有什么想法吗?..

更新 2:这个问题我仍然遇到问题,编码现在打开和关闭 utf8,并且我怀疑 phpmyadmin 以某种方式重置了编码。我找到了一种新的做事方式,并且效果完美,如下面我的回答所述......

I have some forms, that insert some data into a MySQL database, and for some reason the characters get double utf-8 encoded. You don't see it on the front-end of my website, but in the back-end you do, if i look at the data from phpmyadmin, it's double encoded.

Also, to display data entered from phpmyadmin i have to utf8_encode it.

If i use uft8_decode() on my data before i put it into my database, it works, but then i'd have to use utf8_encode() again to display my data properly, and i would like to find a better solution that re-writing most of my code.

The characters i'm dealing with is the danish æ, ø and å characters.

I have every setting i can find in php.ini set to utf-8, every thing i can find in phpmyadmin to utf8, html meta tag set to utf-8, and still i have this error to deal with.

So my question is, does anyone know why this happens, or how i could fix it?..

Update: After running the mysql code Jako suggested, the data is properly encoded in the back-end of the database when it comes from the front-end, but i still need to run utf8_encode() to display the data properly on the front-end, any ideas?..

Update 2: Again, after running the code from the answer to this question i still had problems, the encoding was now on and off utf8, and i suspect phpmyadmin for resetting the encoding somehow. I found a new way of doing things, and it works flawlessly, described in my answer below...

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

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

发布评论

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

评论(2

羁拥 2024-11-11 11:28:35

我过去遇到过类似的问题,这对我来说很有效。

mysql_query("SET names UTF8");

I ran into similar issues in the past and this did the trick for me.

mysql_query("SET names UTF8");
回忆追雨的时光 2024-11-11 11:28:35

好的,我找到答案了!!我搜索了更多一点(已经这样做了几个小时)并发现了这个问题: 是否使用“SET NAMES”

使用该问题的答案,我运行了这个查询:

mysql_query("SET character_set_client = UTF8");
mysql_query("SET character_set_results = UTF8");
mysql_query("SET character_set_connection = UTF8");
mysql_query("SET names UTF8");

就是这样,它现在在我的所有 php 脚本上运行良好。仍然感谢 Jako 引导我走上正确的道路。 ;)

更新: 好的,由于编码打开和关闭并且设置没有保持这种状态,我找到了一个有效的新解决方案。 mysql_set_charset() 添加到我的连接脚本中:

mysql_set_charset("UTF8");

我将 我每次都从数据库中获取正确的数据并插入正确的数据,所以这只是一行代码。

Okay, i found the answer!! I searched a bit more, (have been doing so for hours) and found this question: Whether to use “SET NAMES”

Using the answer from that question i ran this query:

mysql_query("SET character_set_client = UTF8");
mysql_query("SET character_set_results = UTF8");
mysql_query("SET character_set_connection = UTF8");
mysql_query("SET names UTF8");

That's it, it works fine now on all my php scripts. Still thanks to Jako for leading me in the right way. ;)

Update: Okay, since the encoding was on and off and the settings didn't stay that way i found a new solution that works. I added mysql_set_charset() to my connection script:

mysql_set_charset("UTF8");

It gives me the right data from the database every time and inserts the right data as well, so this was only one line of code.

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