daliaessam
daliaessam

Reputation: 1666

Steps for saving and retriving utf8 to MySQL with Perl

I am confused about the Perl DBI settings for handling utf8:

$db->{mysql_enable_utf8} = 1
$db->do(qq{SET NAMES utf8});

I read I should issue them immediately after making the db connection this way:

my $db = DBI->connect($cstring, $user, $password);
$db->{mysql_enable_utf8} = 1
$db->do(qq{SET NAMES utf8});

Here is the issue:

1)-I have the web page with form which set to utf8, so user data are sent in utf8 to the script.

2)-the script uses CGI::Simple to read the form data. Should I decode the form data with utf8::decode() or just leave it?.

3)-Should I set these two or not:

$db->{mysql_enable_utf8} = 1
$db->do(qq{SET NAMES utf8});

I hope some one will explain the steps to save and read utf8 starting from getting the user input on the web page to the MySQL database.

Upvotes: 1

Views: 195

Answers (1)

daliaessam
daliaessam

Reputation: 1666

I did some tests which may be helpful in answering part of the question.

Consider this variable in utf8 Arabic:

my $string = "السلام عليكم";

According to this uft8 counter:

https://mothereff.in/byte-counter

It is 12 characters, totaling 23 bytes.

These two statements

$strlen = length(($string));
say $strlen;

$strlen = length(decode_utf8($string));
say $strlen;

Prints 12, so Perl knows it is utf-8 characters because I've used use utf8; to tell perl my source code is encoded using utf-8. This is the equivalent of decoding your CGI inputs.

Now to the Mysql:

1)- I created test table with attributes:

ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci

2)- Setting these two as following after connecting to the mysql db:

$dbh->{'mysql_enable_utf8'} = 1;
$dbh->do('SET NAMES utf8');

When I see the table in MySQL Windows Query Browser I see it stored as correct Arabic and total 23 bytes and I can read the text as .

السلام عليكم

3)- Without setting these two:

$dbh->{'mysql_enable_utf8'} = 1;
$dbh->do('SET NAMES utf8');

When I see the table in MySQL Windows Query Browser I see wrong encoded data total 50 bytes.

السلام عليكم

I am using Perl 5.10 on Windows.

This means that we have to set these two settings for correct storing and retrieving of utf8 data with mysql after connecting immediately:

$dbh->{'mysql_enable_utf8'} = 1;
$dbh->do('SET NAMES utf8');

This I think clears part of the question in storing and retrieving data from mysql, still the rest of the question about handling the data starting from receiving data from forms should be decoded first or just used as is.

Mysql Query Browser Outputs

Upvotes: 2

Related Questions