简体   繁体   English

使用PHP + MySQL的utf-8编码问题

[英]Issue with utf-8 encoding using PHP + MySQL

I moved data from MySQL 4 (they were originally set to latin2 encoding ) to MySQL 5 and set encoding to utf-8 . 我将数据从MySQL 4 (它们最初设置为latin2 encoding )移动到MySQL 5并将编码设置为utf-8 It looks good in phpMyAdmin , and utf-8 is okay. 它在phpMyAdmin看起来不错,而utf-8也没关系。 However there are question marks instead of some characters on website! 但是网站上有问号而不是一些字符! The website encoding is also set to utf8 so I dont understand where the problem is. 网站编码也设置为utf8所以我不明白问题出在哪里。

PHP and HTML files are also set to utf8 . PHPHTML文件也设置为utf8

I have no idea... 我不知道...

try query 尝试查询

SET NAMES utf8

before any query in your application 在应用程序中的任何查询之前

On my server, adding these to my php file had no effect: 在我的服务器上,将这些添加到我的php文件中没有任何效果:

ini_set('default_charset','utf-8');
mysql_set_charset('utf8');
header('Content-type: text/html; charset=utf-8');

But everything worked perfectly once I added this to the top of my php file: 但是一旦我将它添加到我的php文件的顶部,一切都很完美:

$mysqli->query("SET NAMES 'utf8'");

Note: I am using encoding utf8_general_ci in my database, but utf8_unicode_ci works the same for me. 注意:我在我的数据库中使用编码utf8_general_ci ,但utf8_unicode_ci对我来说也是一样的。

Hope that helps. 希望有所帮助。

Try setting the MySQL connection to UTF-8: 尝试将MySQL连接设置为UTF-8:

SET NAMES 'utf8'

And send explicit UTF-8 headers, just in case your server has some other default settings: 并发送明确的UTF-8标头,以防您的服务器具有其他一些默认设置:

header('Content-type: text/html; charset=utf-8');

You don't have to set your PHP and HTML files to utf-8. 您不必将PHP和HTML文件设置为utf-8。

You just have to set your output encoding to UTF-8 and the browser will display appropriately. 您只需将输出编码设置为UTF-8,浏览器就会正确显示。

In HTML: 在HTML中:

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

In PHP: 在PHP中:

header('Content-Type: text/html; charset=UTF-8');

When you get a string that is UTF-8 from the MySQL table, it will be UTF-8 all the way to browser output unless you convert the encoding. 当您从MySQL表中获得UTF-8字符串时,除非您转换编码,否则它将一直是UTF-8到浏览器输出。 It's the way that the browser inteprets it. 这是浏览器解释它的方式。

When you show UTF8 characters on a website but tell the browser to interpret them as Latin1 (or Latin2) you see this kind of gibberish: ß 当您在网站上显示UTF8字符但告诉浏览器将其解释为Latin1(或Latin2)时,您会看到这种乱码: ß

When you show Latin1 (or Latin2) characters on a website, but tell the browser to interpret them as UTF8, you see question marks. 当您在网站上显示Latin1(或Latin2)字符,但告诉浏览器将它们解释为UTF8时,您会看到问号。

So my guess is that you switched everything to UTF8 (I mean, you told the DB Engine, the web server and the browser you would be using UTF8), but you didn't actually convert the strings to UTF8. 所以我的猜测是你把所有内容都改为UTF8(我的意思是,你告诉数据库引擎,网络服务器以及你将使用UTF8的浏览器),但实际上并没有将字符串转换为UTF8。

Do what @Darkerstar said. 做什么@Darkerstar说。 Convert your dump to UTF8 (Notepad++ can do that easily) and import it again. 将转储转换为UTF8(Notepad ++可以轻松完成)并再次导入。

Put .htaccess file in your web-site root with content: AddDefaultCharset UTF-8 将.htaccess文件放在您的网站根目录中,内容为:AddDefaultCharset UTF-8

and

in your dbconfig set after connection to db: 在连接到db之后的dbconfig集中:

mysql_query("SET NAMES 'utf8'"); mysql_query(“SET NAMES'utf8'”);

mysql_query("SET NAMES UTF8");

adding this line at the end of my "connection.php" solved my problem. 在我的“connection.php”末尾添加这一行解决了我的问题。

My connection file's complete code is: 我的连接文件的完整代码是:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_test = "localhost";
$database_test = "test";
$username_test = "username";
$password_test = "password";
$test = mysql_pconnect($hostname_test, $username_test, $password_test) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_query("SET NAMES UTF8");
?>

My database collation is "utf8_general_ci". 我的数据库排序规则是“utf8_general_ci”。

Pages are "dreamweaver default utf8" and "unicode normalisation form=C (Canonical Decomposition)". 页面是“dreamweaver default utf8”和“unicode normalization form = C(Canonical Decomposition)”。

Here is a fix. 这是一个修复。 Set the header to header ('Content-type: text/html; charset=utf-8'); 将标题设置为header ('Content-type: text/html; charset=utf-8'); Then print your content using utf8_decode($content) . 然后使用utf8_decode($content)打印您的内容。 You must have the two to make it work. 你必须有两个才能使它工作。

I had this problem recently (I hope its the same problem you are having), I tried many ways but at the end what worked was really simple. 我最近遇到了这个问题(我希望你遇到同样的问题),我尝试了很多方法,但最后有效的方法非常简单。

Convert your dumped SQL file to UTF-8 format and then import it. 将转储的SQL文件转换为UTF-8格式,然后导入它。

BW: I used Notepad++ for the conversion. BW:我使用Notepad ++进行转换。

It doesn't seem like setting every SQL Database, Table, and Field to UTF-8 in MySQL and is good enough. 它似乎不像在MySQL中将每个SQL数据库,表和字段设置为UTF-8并且足够好。 Very annoying. 很烦人。

Ended up forcing the issue to solve encoding problems: 结束强制解决编码问题的问题:

Had to use this Every place I open the database: $db->set_charset("utf8"); 不得不使用这个我打开数据库的地方:$ db-> set_charset(“utf8”);

And that worked. 这很有效。 Finally. 最后。

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

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