UTF-8 问题 PHP/MySQL

2021-12-28 00:00:00 utf-8 php mysql

我一直使用 ISO-8859-1 编码,但我现在转而使用 UTF-8.

I've always used ISO-8859-1 encoding, but I'm now going over to UTF-8.

不幸的是,我无法让它工作.

Unfortunately I can't get it to work.

我的 MySQL 数据库是 UTF-8,我的 PHP 文档是用 UTF-8 编码的,我设置了一个 UTF-8 字符集,但它仍然不起作用.

My MySQL DB is UTF-8, my PHP document is encoded in UTF-8, I set a UTF-8 charset, but it still doesn't work.

(像 æ/ø/å 这样的特殊字符不起作用)

(it is special characters like æ/ø/å that doesn't work)

希望大家帮帮忙!

推荐答案

确保与数据库的连接也使用此字符集:

Make sure the connection to your database is also using this character set:

$conn = mysql_connect($server, $username, $password);
mysql_set_charset("UTF8", $conn);

根据 php.net 的 mysql_set_charset 文档:

According to the documentation of mysql_set_charset at php.net:

Note:
This is the preferred way to change the charset. Using mysql_query() to execute 
SET NAMES .. is not recommended.

另见:http://nl3.php.net/manual/en/function.mysql-set-charset.php

检查您当前连接的字符集:

Check the character set of your current connection with:

echo mysql_client_encoding($conn);

另见:http://nl3.php.net/manual/en/function.mysql-client-encoding.php

如果您完成了这些操作并在表格中添加了奇怪的字符,您会看到它显示正确.

If you have done these things and add weird characters to your table, you will see it is displayed correct.

相关文章