utf8 enconding in Perl and MySql

2019-04-04 07:01发布

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');

Any help is appreaciated !

Thanks.

标签: mysql perl utf-8
4条回答
Summer. ? 凉城
2楼-- · 2019-04-04 07:09

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.

查看更多
趁早两清
3楼-- · 2019-04-04 07:13

You need to set Charset to utf8 in your connection!

charset = utf8
查看更多
ら.Afraid
4楼-- · 2019-04-04 07:19

You need to set mysql_enable_utf8 on connection:

 my($dbh) = DBI->connect(
     'dbi:mysql:test',
     'user',
     'password',
     {
         mysql_enable_utf8 => 1,
     }      
);
查看更多
男人必须洒脱
5楼-- · 2019-04-04 07:28

Here is the complet spec :

http://search.cpan.org/~capttofu/DBD-mysql-4.038/lib/DBD/mysql.pm#mysql_enable_utf8

Additionally, turning on this flag tells MySQL that incoming data should be treated as UTF-8. This will only take effect if used as part of the call to connect(). If you turn the flag on after connecting, you will need to issue the command SET NAMES utf8 to get the same effect.

查看更多
登录 后发表回答