Perl和MySql中的utf8编码 [英] utf8 encoding in Perl and MySql

查看:73
本文介绍了Perl和MySql中的utf8编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库(MySql)具有utf8_general归类.我正在从数据库访问数据并显示一个网页(用Perl开发),它显示的瑞典语字符(ä,å,ö)具有不同的字符.我检查了Mysql数据库,在那里可以看到带有ä,å,ö字符的数据.似乎在访问数据时存在编码问题.连接数据库时,使用以下代码

my($dbh) = DBI->connect($config{'dbDriver'},$config{'dbUser'},$config{'dbPass'}) or die "Kunde inte ansluta till $config{'dataSource'}: " . $DBI::errstr;
$dbh->{'mysql_enable_utf8'} = 1;
$dbh->do('set names utf8');

解决方案

如果每个ä/å/ö在输出中用两个字节表示,那么也有可能对字符进行了双重编码. (鉴于问题已经显示您正在执行$dbh->{'mysql_enable_utf8'} = 1;,所以我怀疑这是最有可能的情况.)鉴于您正在网页上显示此消息,另一种可能性是该页面可能未指定字符集<head>中的UTF-8是UTF-8,浏览器可能会错误地猜测其使用的字符编码.

仔细研究您的webapp框架,模板系统等,以确保在从数据库中检索值和它们到达用户浏览器之间仅对值进行一次编码.如果正确配置它们,许多框架/模板引擎(例如我通常使用的Dancer和TT的组合)将自动处理输出编码,这意味着如果在输出之前对数据进行了显式编码,则数据将被双重编码. /p>

my database (MySql) has a utf8_general collation. I am accessing data from database and showing a webpage (developed in Perl), it is showing Swedish characters (ä,å,ö) with a different characters. I checked in Mysql database, there I can see the data with ä,å,ö characters in it. It seems, there is a encoding problem while accessing data. While connecting to database, used following code

my($dbh) = DBI->connect($config{'dbDriver'},$config{'dbUser'},$config{'dbPass'}) or die "Kunde inte ansluta till $config{'dataSource'}: " . $DBI::errstr;
$dbh->{'mysql_enable_utf8'} = 1;
$dbh->do('set names utf8');

解决方案

If each ä/å/ö is being represented in the output by two bytes, then it's also possible that you may be double-encoding the characters. (Given that the question already shows you doing $dbh->{'mysql_enable_utf8'} = 1;, I suspect that this is the most likely case.) Another possibility, given that you're displaying this on a web page, is that the page may not be specifying that the charset is UTF-8 in its <head> and the browser could be guessing incorrectly at the character encoding it uses.

Take a close look at your webapp framework, templating system, etc. to ensure that the values are only being encoded once between when they're retrieved from the database and when they reach the user's browser. Many frameworks/template engines (such as the combination of Dancer and TT that I normally use) will handle output encoding automatically if you configure them correctly, which means that the data will be double-encoded if it's explicitly encoded prior to being output.

这篇关于Perl和MySql中的utf8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆