Why do I have to utf8_decode() my MySQL column val

2019-08-22 16:10发布

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.

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

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.

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

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.

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?

3条回答
Juvenile、少年°
2楼-- · 2019-08-22 16:32

If all is in UTF8 remove the "encoding"=>"UTF8" in your database.php connection details:

$conn = mysql_connect($server, $username, $password);
//mysql_set_charset("UTF8", $conn); // REMOVED. ;)
mysql_select_db($database, $conn);
查看更多
可以哭但决不认输i
3楼-- · 2019-08-22 16:34

You can use htmlentities with third parameters to safely encode UTF-8 :

htmlentities("£", ENT_COMPAT, "UTF-8")
查看更多
再贱就再见
4楼-- · 2019-08-22 16:39

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;

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

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

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?

查看更多
登录 后发表回答