How to read Chinese text from a database using DBI?

Viewed 112

I have a database with a list of articles in Chinese and I'm having a problem when I try to do my query to get all the articles names, all I see is ??? and not the Chinese text.

my $db_connection = DBI->connect("DBI:mysql:host=localhost;database=articles", 'root', ''); 
my $con  = $db_connection->prepare( "select * from articles" );

$con->execute;

while (my $infoD = $con->fetchrow_hashref())
{
    $INFO{$infoD->{name}} .=   decode('utf8', $infoD->{name});  
}
1 Answers

declare in your connection string also that it should use utf8

DBI->connect("DBI:mysql:host=localhost;database=articles", 'root', '',{mysql_enable_utf8 => 1}); 

If you have utf8mb4

you have to replace {mysql_enable_utf8 => 1} with {mysql_enable_utf8mb4 => 1}

Edit:

also see here UTF8 all the way

Related