3

我意识到有十几个类似的问题,但是在这种情况下,建议的解决方案都没有。

我在页面上有一个 PHP 变量,初始化为:

$hometeam="Крылья Советов";    //Cyrrilic string

当我在页面上打印出来时,它会正确打印出来。所以echo $hometeam显示字符串Крылья Советов,它应该。

标头中的内容元标记设置如下:

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">

而且,在页面的最开始,我有以下内容(正如我在搜索中找到的解决方案之一中所建议的那样):

ini_set('default_charset', 'utf-8');

所以这应该很好。

我试图将其保存到的 MySQL 表和有问题的列将 utf8_bin 作为其编码。当我转到 phpMyAdmin 并手动输入 Крылья Советов 时,它会正确保存在该字段中。

但是,当我尝试通过页面上的查询保存它时,使用以下基本查询:

mysql_query("insert into tablename (round,hometeam) values ('1','$hometeam') ");

mysql 条目如下所示:

c390c5a1c391e282acc391e280b9c390c2bbc391c592c391c28f20c390c2a1c390c2bec390c2b2c390c2b5c391e2809ac390c2bec390c2b2

那么这里发生了什么?如果页面上一切正常,并且 MySQL 本身一切正常,那么问题出在哪里?有什么我应该添加到查询本身以使其保持字符串 UTF-8 编码的吗?

请注意,我mysql_set_charset('utf8');在连接到数据库后(在页面顶部)进行了设置。

编辑:运行查询SHOW VARIABLES LIKE "%character_set%"给出以下内容:

Variable_name   Value
character_set_client    utf8
character_set_connection    utf8
character_set_database  latin1
character_set_filesystem    binary
character_set_results   utf8
character_set_server    latin1
character_set_system    utf8
character_sets_dir  /usr/share/mysql/charsets/

似乎这里可能有一些东西,因为该列表中有 2 个 latin1。你怎么看?

此外,当我直接在 phpMyAdmin 中键入西里尔字母字符串时,它起初看起来很好(保存后它会正确显示)。但是重新加载表格,它以十六进制显示,就像插入的表格一样。对于问题中有关此问题的错误信息,我深表歉意。事实证明,这应该意味着问题出在 phpMyAdmin 或数据库本身。

编辑#2:这是show create table tablename返回的:

CREATE TABLE `tablename` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `round` int(11),  `hometeam` varchar(32) COLLATE utf8_bin NOT NULL,  `competition` varchar(32) CHARACTER SET latin1 NOT NULL DEFAULT 'Russia',  PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=119 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
4

4 回答 4

2

Do you get this hex string in phpMyAdmin? I suppose when you SELECT the inserted value by e.g. PHP or the MySQL console client, you would be given the expected cyrillic UTF8 string.

If so, it's a configuration issue with phpMyAdmin, see e.g. here: http://theyouri.blogspot.ch/2010/12/phpmyadmin-collated-db-in-utf8bin-shows.html

phpMyAdmin collated db in utf8_bin shows hex data instead of UTF8 text

$cfg['DisplayBinaryAsHex'] = false;

Moreover, please don't use mysql_query that way, since you're totally open to SQL injections. I'm also not sure if you really want to use utf8_bin, see e.g. this discussion: utf8_bin vs. utf_unicode_ci or this: UTF-8: General? Bin? Unicode?

EDIT There's something weird going on. If you translate the given hex string to UTF8 characters, you get this: "ÐšÑ€Ñ‹Ð»ÑŒÑ Ð¡Ð¾Ð²ÐµÑ‚Ð¾Ð²&quot; (see e.g. http://software.hixie.ch/utilities/cgi/unicode-decoder/utf8-decoder). If you utf8_decode this, you get the desired "Крылья Советов". So, it seems that it's at least utf8 encoded twice (besides the problem that it somewhere shows up as hex characters).

Could you please provide the complete script? Do you utf8_encode your string anywhere? If your script is this and only this (besides a valid, opened MySQL connection):

<?php
$hometeam="Крылья Советов";    //Cyrrilic string
// open mysql connection here
mysql_set_charset('utf8');
mysql_query("INSERT INTO tablename (round, hometeam) VALUES ('1', '$hometeam')");
$result = mysql_query("SELECT * FROM tablename WHERE round = '1'");
$row = mysql_fetch_assoc($result);
echo $row['hometeam'];
?>

And you call the page, what is the result (in the page source of the browser, not what is displayed in the browser)?

Also, please check what happens if you change the collation to utf8_unicode_ci, as suggested in another answer here. That at least covers phpMyAdmin issues when displaying binary data and is propably anyway what you'll want (since you probably want ORDER BY clauses to perform as expected, see discussions in the SO questions I linked above).

EDIT2 Perhaps you could also provide some snippets like SHOW CREATE TABLE tablename or SHOW VARIABLES LIKE "%character_set%". Might help.

于 2013-07-14T19:41:26.000 回答
1

正如我在评论中看到的那样,您无法更新数据库配置,不是吗?

我猜你的编码配置错误,因为我在官方文档MySQL 文档中看到了

我可以向您推荐一个 PHP 解决方案。由于存在很多编码问题,您可以在将字符串插入数据库之前对其进行转换。您必须找到一种通用语言来在 PHP 和数据库之间进行交流。

我在另一个项目中尝试过的一个是使用url_encode($string)and转换字符串url_decode($string)

于 2013-07-17T09:10:41.100 回答
1

此外,当我直接在 phpMyAdmin 中输入西里尔文字符串时,它起初看起来很好(保存后它会正确显示)。但是重新加载表格,它以十六进制显示,就像插入的表格一样。

这几乎可以肯定看起来您的表中存在问题!运行show create table tablename。我敢打赌有 latin1 而不是 utf8,因为您已将其设置为character_set_database变量中的默认值。

要更改此设置,请运行以下命令:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;

会将您的所有varchar字段转换为 utf8。但是要小心您已经在表中拥有的记录,因为它们已经格式错误,如果您将它们转换为 UTF8,它们将保持格式错误。也许最好的办法是重新创建数据库,只需在表定义的末尾添加以下命令:

CREATE TABLE `tablename` (
    ....
) ENGINE=<whatever you use> DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
于 2013-07-17T09:51:09.927 回答
1

1) 尝试使用 PhpMyAdmin 将条目保存到数据库中,然后在 PhpMyAdmin 中查看结果。它看起来好吗?如果是,则数据库已正确创建和设置。

2)尝试utf8_general_ci改用。这应该没关系,但试一试。

3) 在 PHP 端调整所有必要的设置 - 关注这篇文章: http ://blog.loftdigital.com/blog/php-utf-8-cheatsheet 。尤其是试试这个技巧:

echo htmlentities($hometeam, ENT_QUOTES, 'UTF-8')
于 2013-07-16T15:14:04.130 回答