为什么MySQL和Perl中汉字显示异常?
我使用 Perl ORM Rose::DB::Object 和 MySQL 表/列排序规则 utf8_general_ci
。保存数据的代码:
Motorcycle->new(
type => $self->param('type'),
brand => $self->param('brand'),
color => $self->param('color')
)->save;
Mojolicious 中检索数据的代码(该代码相当于 utf8::decode
):
<td><%= Mojo::ByteStream->new($cycle->type)->decode('utf-8') %></td>
<td><%= Mojo::ByteStream->new($cycle->brand)->decode('utf-8') %></td>
<td><%= Mojo::ByteStream->new($cycle->color)->decode('utf-8') %></td>
中文字符在我的应用程序中看起来很好,但在 phpMyAdmin 中看起来很奇怪。如果我将这些字符保存在 phpMyAdmin 中,它们在 phpMyAdmin 中会很好,但在我的应用程序中看起来很奇怪。
我已经在 SQLite 和 Firefox SQLite 附加组件中对其进行了测试,字符在 Firefox SQLite 附加组件和我的应用程序中看起来都很好。
我认为这是服务器问题。如果我将 Perl 与 PHP 融合,那可能会变成一场灾难。
感谢您的帮助。
解决如下:
__PACKAGE__->use_private_registry;
__PACKAGE__->default_connect_options( mysql_enable_utf8 => 1 );
__PACKAGE__->register_db(
driver => 'mysql',
database => 'test',
host => 'localhost',
username => 'root',
password => '',
重要的是添加此行:
__PACKAGE__->default_connect_options( mysql_enable_utf8 => 1 );
读回数据时无需解码。
I use the Perl ORM Rose::DB::Object and the MySQL table/column collation utf8_general_ci
. Code for saving data:
Motorcycle->new(
type => $self->param('type'),
brand => $self->param('brand'),
color => $self->param('color')
)->save;
Code for retrieving data in Mojolicious (the code is equivalent to utf8::decode
):
<td><%= Mojo::ByteStream->new($cycle->type)->decode('utf-8') %></td>
<td><%= Mojo::ByteStream->new($cycle->brand)->decode('utf-8') %></td>
<td><%= Mojo::ByteStream->new($cycle->color)->decode('utf-8') %></td>
Chinese characters look fine in my application, but they seem strange in phpMyAdmin. If I save the characters in phpMyAdmin, they'll be fine in phpMyAdmin but look strange in my application.
I have tested it in SQLite and with the Firefox SQLite add-on, and the characters look fine in both the Firefox SQLite add-on and my application.
I think this is a server issue. If I fuse Perl with PHP, it can become a disaster.
Thanks for help.
RESOLVED as below:
__PACKAGE__->use_private_registry;
__PACKAGE__->default_connect_options( mysql_enable_utf8 => 1 );
__PACKAGE__->register_db(
driver => 'mysql',
database => 'test',
host => 'localhost',
username => 'root',
password => '',
Its important to add this line in:
__PACKAGE__->default_connect_options( mysql_enable_utf8 => 1 );
No decode needed when reading back the data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Perl 代码中,您需要确保 Encode::decode() 输入,以便您的数据采用 Perl 的内部字符表示形式,处理您的数据,然后在写入数据库之前对输出进行 Encode::encode() 编码。您可能还想做:
连接到数据库后,以便 MySQL 客户端库和服务器在字符集上达成一致。
请参阅:http://perldoc.perl.org/perlunitut.html 和 http://perldoc.perl.org/perlunicode.html
In Perl code you need to be sure that you Encode::decode() input so your data are in perl's internal character representation, work with your data, then Encode::encode() your output before writing to the database. You will probably also want to do:
after you connect to the database so that the MySQL client library and server agree on character set.
see: http://perldoc.perl.org/perlunitut.html and http://perldoc.perl.org/perlunicode.html
如果没有看到你的 MySQL 连接代码,很难确定,但最有可能的是,你需要告诉 MySQL 你的连接将使用 UTF-8 - 否则,它会将你的字节解释为其他字符集,这可以解释为什么 phpMyAdmin没有向您显示预期的结果。
对于具有默认 mysql 库的 PHP,这就是 mysql_set_charset 的用途(假设您使用的是普通的旧 mysql_query);对于 mysqli,您有等效的 mysqli_set_charset。
如果您使用其他库来访问 MySQL,则必须查看其文档以获取类似的内容。 Perl 也是如此;我对那种语言一无所知,所以我无法给你任何细节。
Without seeing your MySQL connection code, it's hard to be certain, but most likely, you need to tell MySQL that your connection is going to use UTF-8 - otherwise, it interprets your bytes as some other character set, which would explain why phpMyAdmin isn't showing you the expected result.
For PHP with the default mysql library, that's what the mysql_set_charset is for (assuming you're using plain old mysql_query); for mysqli, you have the equivalent mysqli_set_charset.
If you use some other library to access MySQL, you'll have to look at its documentation for something similar. The same goes for perl; I don't know anything about that language, so I can't give you any specifics.