使用 PHP + MySQL 的 utf-8 编码问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1707792/
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
Issue with utf-8 encoding using PHP + MySQL
提问by Adriana
I moved data from MySQL 4(they were originally set to latin2 encoding) to MySQL 5and set encoding to utf-8. It looks good in phpMyAdmin, and utf-8is okay. However there are question marks instead of some characters on website! The website encoding is also set to utf8so I dont understand where the problem is.
我将数据从MySQL 4(它们最初设置为latin2 encoding)移动到MySQL 5并将编码设置为utf-8. 它看起来不错phpMyAdmin,utf-8还可以。但是网站上有问号而不是一些字符!网站编码也设置为utf8所以我不明白问题出在哪里。
PHPand HTMLfiles are also set to utf8.
PHP和HTML文件也设置为utf8。
I have no idea...
我不知道...
回答by Valentin Golev
try query
尝试查询
SET NAMES utf8
before any query in your application
在您的应用程序中的任何查询之前
回答by Jonathan Cross
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_ciin my database, but utf8_unicode_ciworks the same for me.
注意:我utf8_general_ci在我的数据库中使用编码,但utf8_unicode_ci对我来说是一样的。
Hope that helps.
希望有帮助。
回答by Franz
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');
回答by mauris
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. It's the way that the browser inteprets it.
当您从 MySQL 表中获得 UTF-8 字符串时,除非您转换编码,否则它一直到浏览器输出都是 UTF-8。这是浏览器解释它的方式。
回答by Sebastián Grignoli
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(我的意思是,您告诉 DB 引擎、Web 服务器和浏览器您将使用 UTF8),但实际上并没有将字符串转换为 UTF8。
Do what @Darkerstar said. Convert your dump to UTF8 (Notepad++ can do that easily) and import it again.
做@Darkerstar 所说的。将您的转储转换为 UTF8(Notepad++ 可以轻松完成)并再次导入。
回答by Fahri Akar
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 默认utf8”和“unicode normalization form=C(Canonical Decomposition)”。
回答by Aliaksandr Harbunou
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:
在连接到数据库后的 dbconfig 设置中:
mysql_query("SET NAMES 'utf8'");
mysql_query("SET NAMES 'utf8'");
回答by Rokhayakebe
Here is a fix. Set the header to header ('Content-type: text/html; charset=utf-8');Then print your content using utf8_decode($content). You must have the two to make it work.
这是一个修复。将标题设置为header ('Content-type: text/html; charset=utf-8');然后使用 打印您的内容utf8_decode($content)。您必须同时拥有两者才能使其工作。
回答by user3314053
It doesn't seem like setting every SQL Database, Table, and Field to UTF-8 in MySQL and is good enough. Very annoying.
在 MySQL 中似乎并没有将每个 SQL 数据库、表和字段都设置为 UTF-8,并且已经足够好了。很烦人。
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.
那奏效了。最后。
回答by Darkerstar
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++ 进行转换。

