Re: Perl, DBI and encoding problem

[email protected] (Martin Evans)
Newsgroups perl.dbi.users
Organization Easysoft Limited
Message-ID <[email protected]>
Alex Bernier wrote:
> Hello,
> 
> I use DBI 1.605, the DBD MySQL driver 4.007, Perl 5.10.0, MySQL 5.1.41 (on a Debian Lenny).
> 
> Here is my script :
> 
> <script>
> use DBI;
> 
> $DB_name    = 'xxx';
> $DB_user    = 'xxx';
> $DB_pwd     = 'xxx';
> my $dbh;
> $dbh = DBI->connect("dbi:mysql:dbname=$DB_name","$DB_user","$DB_pwd", {mysql_enable_utf8=>1});
> $dbh->do("set NAMES 'utf8'");
> my $sth = $dbh->prepare("select foo from bar");
> $sth->execute();
> my $line = $sth->fetchrow_hashref;
> print $line->{foo};
> </script>
> 
> This script give a string encoded in Latin1. Because my database use UTF8, I am waiting a string in UTF8. Am I wrong, or is the behaviour of this script not correct ?
> 
> Here are some MySQL variables :
> Variable_name   Value
> character_set_client    utf8
> character_set_connection        utf8
> character_set_database  utf8
> character_set_filesystem        binary
> character_set_results   utf8
> character_set_server    utf8
> character_set_system    utf8
> character_sets_dir      /usr/share/mysql/charsets/
> 
> And in my MySQL config file : 
> [mysqld]
> init-connect = 'SET NAMES utf8'
> character-set-server=utf8
> collation-server=utf8_general_ci
> character_set_client=utf8
> default-character-set = utf8
> 
> 
> 
> When I look a dump file in hexadeciaml of my base, values are encoded in UTF8... 
> In the dump :
> SET @saved_cs_client     = @@character_set_client;
> SET character_set_client = utf8;
> CREATE TABLE `bar` ( [...] ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
> SET character_set_client = @saved_cs_client;
> 
> Thanks in advance for any hint,
> 
> Alex
> 
> 

I no longer use mysql but in the past I successfully used it with UTF-8
encoded data. The only thing I'd question from the above is how do you
know mysql is returning Latin1 compared with UTF-8 encoded data in the
above example? You print it to stdout and if that does not have the
UTF-8 layer on it it will be output differently (e.g., binmode(STDOUT,
":utf8")).

Martin
-- 
Martin J. Evans
Easysoft Limited
http://www.easysoft.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.