UTF-8 问题 PHP/MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4623733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:43:56  来源:igfitidea点击:

UTF-8 problems PHP/MySQL

phpmysqlutf-8

提问by Mikkel

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

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

Unfortunately I can't get it to work.

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

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.

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

(it is special characters like ?/?/? that doesn't work)

(像 ?/?/? 这样的特殊字符不起作用)

Hope you guys can help!

希望大家帮帮忙!

回答by Rene Terstegen

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

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

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

According to the documentation of mysql_set_charsetat php.net:

根据mysql_set_charsetphp.net的文档:

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

See also: http://nl3.php.net/manual/en/function.mysql-set-charset.php

另见: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);

See also: http://nl3.php.net/manual/en/function.mysql-client-encoding.php

另见: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.

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

回答by Mchl

Remember to set connection encoding to utf8 as well.

请记住也将连接编码设置为 utf8。

In ext\mysqli do $mysqli->set_charset("utf8")

在 ext\mysqli 中做 $mysqli->set_charset("utf8")

In ext\mysql do mysql_set_charset("utf8")

在 ext\mysql 中做 mysql_set_charset("utf8")

With other db extensions you might have to run query like SET NAMES 'utf8'

使用其他 db 扩展,您可能必须像这样运行查询 SET NAMES 'utf8'

Some more details about connection encoding in MySQL

有关 MySQL 中连接编码的更多详细信息

As others point out, making sure your source code is utf-8 encoded also helps. Pay special attention to not having BOM (Byte Order Mark) - it would be sent to browser before any code is executed, so using headers or sessions would become impossible.

正如其他人指出的那样,确保您的源代码是 utf-8 编码也有帮助。特别注意不要有 BOM(字节顺序标记)——它会在执行任何代码之前发送到浏览器,因此使用标头或会话将变得不可能。

回答by Edmhs

After connecting to db, run query SET NAMES UTF8

连接到数据库后,运行查询 SET NAMES UTF8

$db = new db(...);
$db->query('set name utf8');

and add this tag to header

并将此标签添加到标题

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

回答by Saleha A.Latif

I had the same problem but now its resolved. Here is the solution:

我有同样的问题,但现在它解决了。这是解决方案:

1st: update ur table ALTER TABLE tbl_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

第一:更新你的表 ALTER TABLE tbl_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

2nd: add this in the head section of the HTML code:

第二:在 HTML 代码的 head 部分添加:

Regards Saleha A.Latif

问候 Saleha A.Latif

回答by kynnysmatto

Nowadays PDO is the recommended way to use mysql. With that you should use the connection string to set encoding. For example: "mysql:host=$host;dbname=$db;charset=utf8"

如今,PDO 是使用 mysql 的推荐方式。有了它,您应该使用连接字符串来设置编码。例如:“mysql:host=$host;dbname=$db;charset=utf8”

回答by Gerard Banasig

Are you having this error? MySql SELECT UNION Illegal mix of collations Error? Just set you entire mysql to utf 8 then

你有这个错误吗?MySql SELECT UNION 排序规则的非法混合错误?只需将整个 mysql 设置为 utf 8 然后

SET character_set_connection = utf8;

回答by Diablo

Try this after connecting to mysql:

连接到mysql后试试这个:

mysql_query("SET NAMES 'utf8'");

And encode PHP document in UTF-8 without BOM.

并在没有 BOM 的情况下UTF-8编码 PHP 文档。