3

I realize there's a dozen similar questions, but none of the solutions suggested there work in this case.

I have a PHP variable on a page, initialized as:

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

When I print it out on the page, it prints out correctly. So echo $hometeam displays the string Крылья Советов, as it should.

The content meta tag in the header is set as follows:

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

And, at the very beginning of the page, I have the following (as suggested in one of the solutions found in my search):

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

So that should be all good.

The MySQL table I'm trying to save this to, and the column in question, have utf8_bin as their encoding. When I go to phpMyAdmin and manually enter Крылья Советов, it saves properly in the field.

However, when I try to save it through a query on the page, using the following basic query:

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

The mysql entry looks like this:

c390c5a1c391e282acc391e280b9c390c2bbc391c592c391c28f20c390c2a1c390c2bec390c2b2c390c2b5c391e2809ac390c2bec390c2b2

So what's going on here? If everything is ok on the page, and everything is ok with MySQL itself, where is the issue? Is there something I should add to the query itself to make it keep the string UTF-8 encoded?

Note that I have set mysql_set_charset('utf8'); after connecting to the database (at the top of the page).

EDIT: Running the query SHOW VARIABLES LIKE "%character_set%" gives the following:

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/

Seems like there could be something here, since there are 2 latin1's in that list. What do you think?

Also, when I type a Cyrillic string directly into phpMyAdmin, it appears fine at first (it displays correctly after I save it). But reloading the table, it displays in HEX like the inserted ones. I apologize for the misinformation regarding this in the question. As it turns out, this should mean the problem is with phpMyAdmin or the database itself.

EDIT #2: this is what show create table tablename returns:

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

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 に答える
1

コメントで見たように、データベース構成を更新できるように縫い付けていませんか?

公式ドキュメントMySQLドキュメントで見たので、エンコーディングの設定が間違っていると思います

PHPソリューションを提案できます。多くのエンコーディングの問題があるため、データベース内に挿入する前に文字列を変換できます。PHP とデータベースの間で対話するための共通言語を見つける必要があります。

他のプロジェクトで試したものは、 and を使用した変換文字列で構成されていurl_encode($string)ますurl_decode($string)

于 2013-07-17T09:10:41.100 に答える