简体   繁体   English

UTF-8 MySQL和Charset

[英]UTF-8 MySQL and Charset

Can someone explain me when I set everything to UTF-8 I keep getting those damn 有人解释我,当我把所有东西都设置为UTF-8时,我会继续得到那些该死的

MySQL MySQL的
Server version: 5.1.44 服务器版本:5.1.44
MySQL charset: UTF-8 Unicode (utf8) MySQL charset:UTF-8 Unicode(utf8)

I create a new database 我创建了一个新的数据库

name: utf8test 名称:utf8test
collation: utf8_general_ci 整理:utf8_general_ci
MySQL connection collation: utf8_general_ci MySQL连接排序规则:utf8_general_ci

My SQL looks like this: 我的SQL看起来像这样:

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

CREATE TABLE IF NOT EXISTS `test_table` (
    `test_id` int(11) NOT NULL,
    `test_text` text NOT NULL,
    PRIMARY KEY (`test_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `test_table` (`test_id`, `test_text`) VALUES
(1, 'hééélo'),
(2, 'wööörld');

My PHP / HTML: 我的PHP / HTML:

<?php
$db_conn = mysql_connect("localhost", "root", "") or die("Can't connect to db");
mysql_select_db("utf8test", $db_conn)  or die("Can't select db");

// $result = mysql_query("set names 'utf8'"); // this works... why??
$query = "SELECT * FROM test_table";        
$result = mysql_query($query);

$output = "";
while($row = mysql_fetch_assoc($result)) {
    $output .= "id: " . $row['test_id'] . " - text: " . $row['test_text'] . "<br />";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="it" xmlns="http://www.w3.org/1999/xhtml" xml:lang="it">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UTF-8 test</title>
</head>
<body>
<?php echo $output; ?>
</body>
</html>

Try to set charachter encoding after mysql_connect function like this: 尝试在mysql_connect函数之后设置charachter编码,如下所示:

 mysql_query ("set character_set_client='utf8'"); 
 mysql_query ("set character_set_results='utf8'"); 

 mysql_query ("set collation_connection='utf8_general_ci'");

I didn't see a "SET NAMES 'utf8';" 我没有看到"SET NAMES 'utf8';" query just after connecting to your database. 在连接到数据库之后查询。

Try it, may work for you. 试试吧,可能适合你。

I set everything to UTF-8 我把所有东西都设置为UTF-8

Not quite. 不完全的。
You have to tell mysql your client 's encoding. 你必须告诉mysql 客户端的编码。

As a matter of fact, you don't have to set up "everything" in utf-8. 事实上,你不必在utf-8中设置“所有东西”。 You can have your tables in latin1 and output in utf-8. 您可以将表放在latin1中并输出为utf-8。 Or contrary. 或者相反。
Very flexible. 非常灵活。

But you have to set up client's encoding explicitly. 但是你必须明确地设置客户端的编码。 So, that's why it works with set names utf8 . 所以,这就是为什么它适用于set names utf8 Because this query setting up client's encoding. 因为此查询设置了客户端的编码。 And let Mysql know that data must be sent in utf-8. 让Mysql知道必须在utf-8中发送数据。 Pretty sensible, huh? 非常明智,对吧?

Also I have to mention your SQL dump. 另外我还要提一下你的SQL转储。 It needs same setting. 它需要相同的设置。 Just SET NAMES somewhere at the top. 只是在顶部的某处设置名称。 Because you are sending these queries from some client too. 因为您也是从某个客户端发送这些查询。 And this client's encoding needs to be set up as well. 此客户端的编码也需要设置。

And one more thing to mention: be sure your server sending proper encoding in the Content-type header. 还有一件事需要提及:确保您的服务器在Content-type标头中发送正确的编码。 You didn't set it to UTF-8 too. 你没有将它设置为UTF-8。

I would say that you forget to set the content type encoding of your PHP file to utf-8: 我会说你忘记将PHP文件的内容类型编码设置为utf-8:

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

Or is the encoding error within the MySQL database? 或者是MySQL数据库中的编码错误?

If only loading the data returns the wrong results, you can use the queries mentioned before or this line of code to enable UTF-8 for queries: 如果仅加载数据会返回错误的结果,则可以使用前面提到的查询或此行代码为查询启用UTF-8:

$mysqli->set_charset('utf8');

I hope that is what you needed. 我希望这就是你所需要的。

take a look at the mysql_set_charset function. 看一下mysql_set_charset函数。 Perhaps you need to call it before you retreive the data. 也许您需要在检索数据之前调用它。

You want to check the current charset using mysql_client_encoding and when needed mysql_set_charset . 您想使用mysql_client_encoding检查当前字符集,并在需要时检查mysql_set_charset Or just never mind the checking and blindly go with setting. 或者只是不介意检查,盲目地去设置。

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

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