Utf8 in web perl application (LAMP) – dbi, mysql

Wednesday 23 December 2009 @ 6:59 am

Horror with utf8 and LAMP ( perl ) web application contiunued:

We need to take care of  mysql connection, so it is ut8 – ready:

if (my $dbh = DBI->connect(“DBI:mysql:database=”.$name.’;host=’.$hostname, $user, $password,
{
RaiseError        => $raise,
#             AutoCommit        => 1,
mysql_enable_utf8 => 1,
on_connect_do => [ “SET NAMES ‘utf8′”, “SET CHARACTER SET +’utf8′” ],
})) {

$dbh->{‘mysql_enable_utf8’} = 1;
return $dbh; # DBI database handler
}

We also must make sure that our tables are unicode:

CREATE TABLE `foo` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
….

)  DEFAULT CHARSET=utf8

And remember, that utf8 chars may take more space than normal – so prepare longer varchars in tables and be prepared for problem with indexes, like this: Specified key was too long; max key length is 1000 bytes – see http://bugs.mysql.com/bug.php?id=4541

Tags: ,

Comments (0) - Posted in work by  




Warning: Creating default object from empty value in /home3/lech/public_html/baczynski.com/perl/wp-includes/comment-template.php on line 1057

Leave a comment