Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: A UTF8 round trip with MySQL

by Burak (Chaplain)
on Jun 27, 2007 at 22:42 UTC ( [id://623764]=note: print w/replies, xml ) Need Help??


in reply to A UTF8 round trip with MySQL

This is what I do when communicating with MySQL (code might not be valid -- some sort of pseudo code --):
$dbh = connect; # fix connection for non-utf8 servers # execute this immediately $dbh->do(" SET character_set_client = utf8; SET character_set_connection = utf8; SET character_set_results = utf8; "); $data = fetch; $data = decode_utf8 $data; binmode STDOUT, ':utf8'; print $data;

Replies are listed 'Best First'.
Re^2: A UTF8 round trip with MySQL
by clinton (Priest) on Jun 28, 2007 at 07:53 UTC
    $data = fetch;
    $data = decode_utf8 $data;
    The bit you're missing here is deciding which data to decode, because of course you need to handle text and binary data differently. The latest DBD::mysql drivers handle this automatically.

    Clint

      Well, since I don't deal with binary data right now, I didn't think about that part :) Ok, I'll check the new interface, but I want to stay compatible with old versions too...
Re^2: A UTF8 round trip with MySQL
by Joost (Canon) on Jun 28, 2007 at 18:01 UTC
      mysql_enable_utf8 is not an auto feature, so it has no effect unless enabled. If it is in effect and I also use Encode methods, this is like shooting yourself in the foot :)
      decode_utf8 will only fail if the input is invalid utf-8. whether the data is flagged as utf-8 or not does not matter.
Re^2: A UTF8 round trip with MySQL
by Anonymous Monk on Mar 30, 2009 at 18:49 UTC
    I use perl -C (or sets the shell environment variabel PERL_UNICODE=""), and then you do not need any utf8 specific parameter in the code unless the column (or table) names are non-ascii. If they are, mysql_enable_utf8 => 1 have to be set.
    $dbh = DBI->connect($dsn, $username, $password, {mysql_enable_utf8 => +1}) || die "Could not connect to $database: $DBI::errstr"; $sth->execute || die "Could not execute statement: ".$sth->errstr."\n" +; $sth = $dbh->prepare("select foo from bar where id = '3064'") || die " +Could not prepare statement: ".$dbh->errstr."\n"; my $string = $sth->fetchrow_array; print $string . "\n";
    Will print utf8 to STDOUT.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-18 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found