in reply to Perl Mysql and UTF8

Are you binding these character strings? If not, try it. If you are, I'd look into whether or not there is some means of specifying the character encoding of the bind parameter. Perhaps like (ficticious examples follow!):
$sth->bind_param(1,$value,SQL_VARCHAR_UTF8)
or perhaps more like
$sth->bind_param(1,$value,{TYPE=>SQL_VARCHAR, ENCODING=>UTF8})
I don't know details, but maybe this can guide your search for further information.

I don't see anything specific about this in the DBI or DBD::mysql docs... but you could try playing around with it a little.


------------
:Wq
Not an editor command: Wq

Replies are listed 'Best First'.
Re: Re: Perl Mysql and UTF8
by emilioayllon (Novice) on Nov 02, 2003 at 03:29 UTC
    I tried using $var = $dbh->quote($var) but unfortunately this didnt seem to cure the problem.

    Furthermore, if I print out the new $var on console the foreign characters are ruined.

    Thanks for trying.

      Oh! Well, that means that it is $dbh->quote(...) that is hosing your data. I would really recommend that you look into using binds for your dynamic literals rather than quoting them.

      If you are not familiar with binds (or "placeholders", depending on the documentation source), then you should really look into them. The basic idea is that, instead of doing this:

      my $quotedvar = $dbh->quote($var); my $sql = "insert into mytable (mycolumn) values ($quotedvar)"; $dbh->do($sql);
      do this:
      my $sql = "insert into mytable (mycolumn) values (?)"; $dbh->do($sql,undef,$var);
      But you should definiftely consult the DBI docs for more info on this (see the section under "Placeholder and Bind Values").

      I'm really not at all surprised that $dbh->quote(...) is mangling your data, because, well, that's what its job is, and apparently it is just getting a little bit over-zealous with its mangling. More to the point, it may not be character-encoding-aware... wheras simply binding the value just passes it directly through to the DBD. And, in the case of mysql, the DBD is probably capable of handling character-encoding well, since there is explicit functinoality around it in the database.


      ------------
      :Wq
      Not an editor command: Wq