Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Mugged by UTF8, this CANNOT be right

by nif (Sexton)
on Jan 26, 2011 at 21:29 UTC ( [id://884424]=note: print w/replies, xml ) Need Help??


in reply to Mugged by UTF8, this CANNOT be right

Just tried to use "$sth->fetchall_arrayref" with UNICODE in mysql table - with "$dbh->{mysql_enable_utf8}=1" it always returns right encoded strings - so no need to encode/decode yourself.
Test details in <readmore> section below...

And about

"foreign accents"
- a lot of Monks, who give you answers on this site, do not speak English at home.

Creation of test mysql database "db1" and test table "tbl1" with unicode content:

mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

mysql> connect db1;
Connection id: 27
Current database: db1

mysql> create table tbl1 (id INT, str CHAR(80) CHARACTER SET utf8);
Query OK, 0 rows affected (0.08 sec)

mysql> insert into tbl1 values (0, 'äöüß');
Query OK, 1 row affected (0.00 sec)

mysql> insert into tbl1 values (1, 'Ƽ®©');
Query OK, 1 row affected (0.00 sec)

mysql> insert into tbl1 values (2, 'ĦǾǺǽ');
Query OK, 1 row affected (0.00 sec)

mysql> insert into tbl1 values (3, 'no unicode');
Query OK, 1 row affected (0.00 sec)

mysql> select * from tbl1;
+------+------------+
| id   | str        |
+------+------------+
|    0 | äöüß       |
|    1 | Ƽ®©       |
|    2 | ĦǾǺǽ       |
|    3 | no unicode |
+------+------------+
3 rows in set (0.00 sec)

Test program:

#!/usr/bin/perl use strict; use warnings; use utf8; use DBI; use Data::Dumper; my $dbh = DBI->connect("dbi:mysql:database=db1","",""); $dbh->{mysql_enable_utf8} = 1; my $sth = $dbh->prepare('select * from tbl1'); $sth->execute; my $ar = $sth->fetchall_arrayref; $sth->finish; print Dumper( $ar ); binmode(STDOUT, ':utf8'); my $str1 = $ar->[1]->[1]; print qq{"$str1" - }, (utf8::is_utf8($str1) ? 'UTF8' : 'ASCII'), qq{ +\n}; my $str3 = $ar->[3]->[1]; print qq{"$str3" - }, (utf8::is_utf8($str3) ? 'UTF8' : 'ASCII'), qq{ +\n};

Output:

$VAR1 = [ [ '0', "\x{e4}\x{f6}\x{fc}\x{df}" ], [ '1', "\x{c6}\x{bc}\x{ae}\x{a9}" ], [ '2', "\x{126}\x{1fe}\x{1fa}\x{1fd}" ], [ '3', 'no unicode' ] ]; "Ƽ®©" - UTF8 "no unicode" - ASCII

Replies are listed 'Best First'.
Re^2: Mugged by UTF8, this CANNOT be right
by rowdog (Curate) on Jan 27, 2011 at 03:59 UTC
    - a lot of Monks, who give you answers on this site, do not speak English at home.

    Especially the monks who know how to answer questions about Unicode!

    Edit: that's a bass ackwards way to extract the minutes anyhow, man date shows that date +'%M' would have sufficed. Oops, this is the wrong post for that totally irrelevant edit.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://884424]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-25 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found