FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Displaying UTF-8-encoded strings from MySQL with PHP
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Displaying UTF-8-encoded strings from MySQL with PHP [message #174522 is a reply to message #174521] Wed, 15 June 2011 14:26 Go to previous messageGo to previous message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma:
Senior Member
In article <itaeu4$lgp$1(at)dont-email(dot)me>,
"輙varo G. Vicario" <alvaro(dot)NOSPAMTHANX(at)demogracia(dot)com(dot)invalid> wrote:

> El 15/06/2011 15:50, Luke escribi�/wrote:
>>>> > // Simply performs a query and returns mysql_fetch_assoc on the first
>>>> > result
>>>> > $hex = $theDB->getResult("SELECT HEX(link_text) FROM language WHERE
>>>> > id=2");
>>>
>>>> > // Echos "C3BC" (which is the correct value for the character in the
>>>> > DB ("�"))
>>>> > echo $hex['HEX(link_text)'];
>>>
>>>> > // Selecting the actual value instead of its hex-represenation
>>>> > $char = $theDB->getResult("SELECT link_text FROM language WHERE
>>>> > id=2");
>>>
>>>> > // Echos "fc" (which is " " (broken character))
>>>> > echo bin2hex($char['link_text']);
>>>
>>>> > I'm a little confused. I tried various functions on this:
>>>
>>>> > $theDB->query("SET NAMES ヤutf8′");
>>>> > mysql_set_charset('utf8');
>>>> > mb_internal_encoding("UTF-8");
>>>
>>>> > mb_internal_encoding("UTF-8");
>>>
>>>> > Nothing helps. Changing configurations is not impossible, but very
>>>> > difficult. Also the database seems fine, as HEX(xx) returns the
>>>> > correct value, and in my PHP-Scripts I can use UTF-8 characters (that
>>>> > aren't loaded from the database) and they work too. There must be
>>>> > something in 'between'…
>>>
>>>> And it seems FC is the "Hex code point" of the character I'm looking
>>>> for. But I actually have no clue what to do. Wikipedia says Hex code
>>>> point marks the range in which characters are assigned to languages
>>>> (more or less), but i'm not sure why that is returned or even how to
>>>> handle this.
>>>
>>> Oh wait FC is the ASCII-Value for �. So the question is, why does PHP
>>> recieve an ASCII-char if the MySQL-Function HEX() returns the correct
>>> UTF-8 hexvalue?
>
>
> ASCII is a 7-bit encoding that does not have non-English letters like
> "�". That FC you get is the ISO-8859-1 code. The data gets converted
> from UTF-8 to ISO-8859-1 (or a similar charset).
>
>> Ok, I got it. As supposed here http://akrabat.com/php/utf8-php-and-mysql/
>> I had to query "SET NAMES UTF8;" right after I connected to the
>> database. No need to change tables or configuring Apache and PHP
>
> Hadn't you tried that already?
>
> http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
>
>> (well, I had to insert meta-tags setting UTF-8 as charset, but apart
>> from that there was nothing else to do).
>
> Meta-tags are not useful unless you save the page to disk from your
> browser and open it later. You should either use header() to set the
> Content-Type header or configure it via .htaccess.

Indeed. Doing this at the start of all my PHP scripts:

header ("Content-Type: text/plain; charset=utf-8");

fixed my char-set problems.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: How do I force PHP to assume UTF-8 for $_GET?
Next Topic: radio button change after going to next page
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 14:39:20 GMT 2024

Total time taken to generate the page: 0.04212 seconds