简体   繁体   English

UTF-8问题PHP / MySQL

[英]UTF-8 problems PHP/MySQL

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! 希望你们能帮忙!

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_charset at php.net: 根据php.net上mysql_set_charset的文档:

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. 如果您已完成这些操作并在表格中添加奇怪的字符,您将看到它显示正确。

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' 使用其他数据库扩展,您可能必须运行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. 正如其他人所指出的,确保源代码是utf-8编码也有帮助。 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. 要特别注意没有BOM(字节顺序标记) - 它会在执行任何代码之前发送到浏览器,因此使用标题或会话将变得不可能。

After connecting to db, run query SET NAMES UTF8 连接到db后,运行查询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" /> 

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; 1st:update ur table 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

Nowadays PDO is the recommended way to use mysql. 现在PDO是推荐使用mysql的方法。 With that you should use the connection string to set encoding. 有了它,您应该使用连接字符串来设置编码。 For example: "mysql:host=$host;dbname=$db;charset=utf8" 例如:“mysql:host = $ host; dbname = $ db; charset = utf8”

Are you having this error? 你有这个错误吗? MySql SELECT UNION Illegal mix of collations Error? MySql SELECT UNION非法混合排序错误? Just set you entire mysql to utf 8 then 只需将整个mysql设置为utf 8即可

SET character_set_connection = utf8;

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文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM