为什么我必须对我的MySQL列值utf8_decode()进行正确显示? [英] Why do I have to utf8_decode() my MySQL column value to get it to display properly?

查看:133
本文介绍了为什么我必须对我的MySQL列值utf8_decode()进行正确显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CakePHP,App.encoding设置为UTF-8,<head>中存在<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />,而我的MySQL数据库设置为UTF-8 Unicode编码和utf8_general_ci排序规则.我的database.php连接详细信息中也有"encoding"=>"UTF8".

I'm using CakePHP with App.encoding set to UTF-8, <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> present in my <head> and my MySQL database set to UTF-8 Unicode Encoding and utf8_general_ci collation. I also have "encoding"=>"UTF8" in my database.php connection details.

当我在数据库表中存储一个'£'符号并使用命令行MySQL查看它时,该字符正确显示.

When I store a '£' symbol in the database table and view it using command line MySQL, the character displays correctly.

如果我使用CakePHP从数据库表中获取行并将其输出到我的网站中,则会看到£而不是预期的£符号.

If I use CakePHP to fetch the rows from the database table and output them in my website, I see £ instead of my intended £ symbol.

但是,如果我随后使用utf8_decode()输出数据,则它会正确显示.

However if I then use utf8_decode() to output my data, it displays correctly.

这是正确的吗?我尝试使用htmlentities()将£符号转换为&pound;,但它改为输出&Acirc;&pound;!即使当我将其他参数用于charset时.

Is this correct? I have tried using htmlentities() to convert the £ symbol into &pound; but it outputs &Acirc;&pound; instead! Even when I use the additional parameters for charset.

也许有人可以帮忙-我肯定在这里错过了一些东西,但是我认为,如果您所有的标头,元标记等始终为UTF-8,字符应该正确显示(在诸如textarea HTML标记之类的东西中)?

Perhaps someone can help - I must have missed something here, but I thought that the characters should display correctly (in things like textarea HTML tags) if all your headers, meta tags etc were consistently UTF-8?

推荐答案

听起来数据库中的数据有误:字符£实际上存储为两个字符£.您可以通过进入数据库并使用十六进制和字符集功能来确认这一点:

It sounds like the data in your database is wrong: the character £ is actually stored as the two characters £. You can confirm this by going to the database and using the hex and charset functions:

select charset(MyColumn), hex(MyColumn) from MyTable;

如果该列以UTF-8编码,则对于值'£',您应该看到与此相同的输出:

If the column is encoded in UTF-8, for the value '£' you should see output identical to this:

+---------------+-----------+
| utf8          | C2A3      |
+---------------+-----------+

如果您看到其他内容,例如charset列报告为latin1或hex列报告为C382C2A3,则表中的数据错误.虽然可以修复,但是修复方法取决于数据的错误类型.您从charsethex那里得到什么?

If you see anything else, like if the charset column reports latin1 or if hex column reports C382C2A3, the data in the table is wrong. It can be fixed though, but the fix depends on the kind of error the data has. What do you get from charset and hex?

这篇关于为什么我必须对我的MySQL列值utf8_decode()进行正确显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆