双UTF-8编码,但为什么呢? PHP/MySQL
我有一些表单,将一些数据插入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我过去遇到过类似的问题,这对我来说很有效。
I ran into similar issues in the past and this did the trick for me.
好的,我找到答案了!!我搜索了更多一点(已经这样做了几个小时)并发现了这个问题: 是否使用“SET NAMES”使用该问题的答案,我运行了这个查询:
就是这样,它现在在我的所有 php 脚本上运行良好。仍然感谢 Jako 引导我走上正确的道路。 ;)更新: 好的,由于编码打开和关闭并且设置没有保持这种状态,我找到了一个有效的新解决方案。 mysql_set_charset() 添加到我的连接脚本中:
我将 我每次都从数据库中获取正确的数据并插入正确的数据,所以这只是一行代码。
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:
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:
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.