|
Posted by YorHel on August 14, 2005, 2:26 am
Please log in for more thread options
YorHel wrote:
> xhoster@gmail.com wrote:
> > > xhoster@gmail.com wrote:
> >
> > > well... I don't get the exact same data when putting something in de
> > > DB, as getting something from the DB, after a lot of testing, this is
> > > the only way I found.
> >
> > I think you maybe you are testing the wrong thing. Since you want Perl to
> > play nice with others, you should do the experimentation on that focus,
> > i.e. getting Perl to read what others have written, rather than getting
> > Perl to read what Perl has written.
> >
> > >
> > > >
> > > > > That works like a charm within perl, the problem is though, that the
> > > > > data isn't really stored as I want it.
> > > >
> > > > How do you know?
> > > >
> > >
> > > Sorry, forgot to mention that the command-line 'mysql' gives me the
> > > same output as phpMyAdmin, and I have added
> > > 'default-character-set=utf8' at /etc/my.cnf, so I can assume that that
> > > client gives me the "real" output.
> >
> > Is this in the [client] section or one of the server sections? (My client
> > can't even read the /etc/my.cnf file, so of course doesn't care what it
> > says.)
>
> $ mysql --help | grep default-character-set
> default-character-set utf8
>
> seems ok...
>
> >
> > If you insert the utf-8 data using phpMyAdmin and then select it with
> > command line mysql, does it show you what you expected?
> >
>
> Yes, phpMyAdmin and cli mysql give me the same results.
>
> > Anyway, if I knew exactly what Perl was inserting[1], what it was getting
> > back[2], what mysql thought it had[3], and what php thought it had, I would
> > be able to offer more help. Or, if you accept php as being the gold
> > standard, insert something with php and then retrieve it with Perl and use
> > syntax [2] on it.
> >
> > > >
> > > > You just reported that your method works like a charm with mysql and
> > > > Perl.
> > > >
> > >
> > > But I also like to use other tools on the same database, so the IS a
> > > problem :)
> >
> > Yes, but I'm not sure who the problem is with :(.
> >
> > [1] print join ",", map ord, split //, $somevar;
> > [2] print join ",", map ord, split //, $result; # do both before and after
> > # the decode call.
> > [3] SELECT ascii(mid(text,1,1)) FROM test WHERE name = 'test'
> > SELECT ascii(mid(text,2,1)) FROM test WHERE name = 'test'
> > SELECT ascii(mid(text,3,1)) FROM test WHERE name = 'test'
> > etc.
> >
>
> $rows = $dbh->do("UPDATE test SET text = '$somevar' WHERE name =
> 'test'");
> print "\n[1] " . join ",", map ord, split //, $somevar;
>
> $obj = $dbh->prepare("SELECT text FROM test WHERE name = 'test'");
> $obj->execute();
> my $result = ($obj->fetchrow_array())[0];
> print "\n[2] be4 " . join ",", map ord, split //, $result;
> $result = decode('UTF-8', $result);
> print "\n[2] aft " . join ",", map ord, split //, $result;
>
> ## Same data, but inserted with phpMyAdmin
> my $obj = $dbh->prepare("SELECT text FROM test WHERE name =
> 'fromphp'");
> $obj->execute();
> my $fromphp = ($obj->fetchrow_array())[0];
> print "\n[2] php " . join ",", map ord, split //, $fromphp;
>
> my $count = 1; my @res;
> while(1) {
> $obj = $dbh->prepare("SELECT ascii(mid(text, $count, 1))" .
> "FROM `test` WHERE name = 'test'");
> $obj->execute();
> my $res = ($obj->fetchrow_array())[0];
> last if !$res;
> push(@res, $res);
> $count++;
> }
> print "\n[3] " . join ",", @res;
>
>
> gives me
>
> [1] 235,228,227,232,223,231,236,297,237,238
> [2] be4
> 195,171,195,164,195,163,195,168,195,159,195,167,195,172,196,169,195,173,195,174
> [2] aft 235,228,227,232,223,231,236,297,237,238
> [2] php 235,228,227,232,223,231,236,63,237,238
> [3]
> 195,194,195,194,195,194,195,194,195,197,195,194,195,194,195,194,195,194,195,194
>
> It seems to me that DBD::mysql just won't send/receive UTF-8, what I
> get from the php-inserted row is almost the same as $somevar, except
> that it doesn't give me all characters (and therefore isn't real UTF-8)
>
Hate to reply to myself, but after some more googling, I found that
when I cal a
$dbh->do("SET NAMES 'utf8'");
right after the DBH->connect(), I can put the real UTF-8-data in the
DB, so phpMyAdmin and cli mysql both give the real UTF-8 output,
instead of those weird characters. With this, it doesn't matter whether
I call the decode() on $somevar before sending the UPDATE query, the
data will still be inserted as UTF-8.
But then again, I need to set the utf8-flag on $result with decode(),
to get the well-formatted data, which sound like a hack to me, am I not
supposed to get the UTF-8-ed data when I call $obj->fetchrow_array()?
And the use of the "SET NAMES 'utf8'"-call also seems like an hack to
me, why do I need to use that when the database I am using is already
defined as "UTF-8".
Ah well, I am glad I have a "solution" to the problem now :)
> Thnx for your help so far :)
>
> > Xho
> >
> > --
> > -------------------- http://NewsReader.Com/ --------------------
> > Usenet Newsgroup Service $9.95/Month 30GB
|