Unix Technical Forum

Mysql database in UTF8, PHP shows latin1 (iso-8859-1)

This is a discussion on Mysql database in UTF8, PHP shows latin1 (iso-8859-1) within the MySQL forums, part of the Database Server Software category; --> I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-07-2008, 08:39 AM
alex
 
Posts: n/a
Default Mysql database in UTF8, PHP shows latin1 (iso-8859-1)

I've converted a latin1 database I have to utf8. The process has been:

# mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore
--skip-set-charset mydb > mydb.sql

# iconv -f ISO-8859-1 -t UTF-8 mydb.sql > mydb_utf8.sql

[then i replaced all CHARSET=latin1 to CHARSET=utf8 in the file, and
checked in the editor that non default characters as accents were viewed
correctly using UTF8 charset]

mysql> CREATE DATABASE mydb_utf8 CHARACTER SET utf8 COLLATE utf8_general_ci;

# mysql -u root -p --default-character-set=utf8 mydb_utf8 < mydb_utf8.sql


I made a simple script in PHP to dump data from tables which had non
standard characters. But the resulting page is viewed correctly in
iso-8859-1 encoding and not in UTF-8.

At this moment i'm clueless were the problem lies.


..alex
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-07-2008, 08:39 AM
Norbert Tretkowski
 
Posts: n/a
Default Re: Mysql database in UTF8, PHP shows latin1 (iso-8859-1)

Am Fri, 04 Apr 2008 10:11:16 +0200 schrieb alex:
> I made a simple script in PHP to dump data from tables which had non
> standard characters. But the resulting page is viewed correctly in
> iso-8859-1 encoding and not in UTF-8.


http://dev.mysql.com/doc/refman/5.0/...onnection.html

I guess using "set names utf8" should help.

Norbert
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-07-2008, 08:39 AM
alex
 
Posts: n/a
Default Re: Mysql database in UTF8, PHP shows latin1 (iso-8859-1)

En/na Norbert Tretkowski ha escrit:
> Am Fri, 04 Apr 2008 10:11:16 +0200 schrieb alex:
>> I made a simple script in PHP to dump data from tables which had non
>> standard characters. But the resulting page is viewed correctly in
>> iso-8859-1 encoding and not in UTF-8.

>
> http://dev.mysql.com/doc/refman/5.0/...onnection.html
>
> I guess using "set names utf8" should help.
>


Yes, it helps, but why should i change my app?

I mean, if the database is set to utf8, the tables are utf8 too (and
just to check, i've also set under [client], [mysqld] and [server] the
value default_character_set=utf8), shouldn't php return me the string as
utf8?

Isn't this a bit bugged?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-07-2008, 08:39 AM
Michael Fesser
 
Posts: n/a
Default Re: Mysql database in UTF8, PHP shows latin1 (iso-8859-1)

..oO(alex)

>En/na Norbert Tretkowski ha escrit:
>> Am Fri, 04 Apr 2008 10:11:16 +0200 schrieb alex:
>>> I made a simple script in PHP to dump data from tables which had non
>>> standard characters. But the resulting page is viewed correctly in
>>> iso-8859-1 encoding and not in UTF-8.

>>
>> http://dev.mysql.com/doc/refman/5.0/...onnection.html
>>
>> I guess using "set names utf8" should help.
>>

>
>Yes, it helps, but why should i change my app?
>
>I mean, if the database is set to utf8, the tables are utf8 too (and
>just to check, i've also set under [client], [mysqld] and [server] the
>value default_character_set=utf8), shouldn't php return me the string as
>utf8?
>
>Isn't this a bit bugged?


Just storing the data as UTF-8 is not enough. You also have to set the
connection between MySQL and your script to UTF-8 and PHP has to send a
correct HTTP header back to the browser.

Micha
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-07-2008, 08:39 AM
alex
 
Posts: n/a
Default Re: Mysql database in UTF8, PHP shows latin1 (iso-8859-1)

En/na Michael Fesser ha escrit:

>
> Just storing the data as UTF-8 is not enough. You also have to set the
> connection between MySQL and your script to UTF-8 and PHP has to send a
> correct HTTP header back to the browser.


Ok, i know that i must send correct headers, but still having this
variables:

+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

The output is ISO, and that is what freaks me out. :P
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-07-2008, 08:39 AM
Jerry Stuckle
 
Posts: n/a
Default Re: Mysql database in UTF8, PHP shows latin1 (iso-8859-1)

alex wrote:
> En/na Michael Fesser ha escrit:
>
>>
>> Just storing the data as UTF-8 is not enough. You also have to set the
>> connection between MySQL and your script to UTF-8 and PHP has to send a
>> correct HTTP header back to the browser.

>
> Ok, i know that i must send correct headers, but still having this
> variables:
>
> +--------------------------+----------------------------+
> | Variable_name | Value |
> +--------------------------+----------------------------+
> | character_set_client | utf8 |
> | character_set_connection | utf8 |
> | character_set_database | utf8 |
> | character_set_filesystem | binary |
> | character_set_results | utf8 |
> | character_set_server | utf8 |
> | character_set_system | utf8 |
> | character_sets_dir | /usr/share/mysql/charsets/ |
> +--------------------------+----------------------------+
>
> The output is ISO, and that is what freaks me out. :P
>


Check Micha's comment again:

"...and PHP has to send a correct HTTP header back to the browser."

The dataset connection and charset have nothing to do with the charset
sent in the page header. You can set the correct charset for the page
in your Apache server or in PHP.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-07-2008, 08:39 AM
alex
 
Posts: n/a
Default Re: Mysql database in UTF8, PHP shows latin1 (iso-8859-1)

En/na Jerry Stuckle ha escrit:

>
> Check Micha's comment again:
>
> "...and PHP has to send a correct HTTP header back to the browser."
>
> The dataset connection and charset have nothing to do with the charset
> sent in the page header. You can set the correct charset for the page
> in your Apache server or in PHP.



It's not a http header problem as i have in my script:

<?php header("Content-type: text/html; charset=utf-8"); ?>

it seems a php problem as when i execute "show variables" in an mysql
console i get:

character_set_client=utf8

but from php i get:

character_set_client=latin1

when the default character set is utf8 (in my.cnf) and i have not
configured it for latin1 anywhere.

I've got 2 more apps one in java and the another one in .Net. None of
those had to be modified in order to retrieve the results. But PHP needs
the "set names utf8" in order to get the proper encoding.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-07-2008, 08:39 AM
Willem Bogaerts
 
Posts: n/a
Default Re: Mysql database in UTF8, PHP shows latin1 (iso-8859-1)

> it seems a php problem as when i execute "show variables" in an mysql
> console i get:
>
> character_set_client=utf8
>
> but from php i get:
>
> character_set_client=latin1


That is a problem I also had. the [mysql] section of my.cnf is for the
command-line client only. A [client] section may or may not be used for
PHP connections. I had to send a "SET NAMES utf8" upon connection to
really convince MySQL to use utf8 for the connection.

This really sucks, off course. It is plain stupid to have to set the
encoding used encoded in the encoding it has to set. It is like sending
a key INSIDE a safe which requires that key to open it.

Best regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-07-2008, 08:39 AM
Michael Fesser
 
Posts: n/a
Default Re: Mysql database in UTF8, PHP shows latin1 (iso-8859-1)

..oO(Willem Bogaerts)

>> it seems a php problem as when i execute "show variables" in an mysql
>> console i get:
>>
>> character_set_client=utf8
>>
>> but from php i get:
>>
>> character_set_client=latin1

>
>That is a problem I also had. the [mysql] section of my.cnf is for the
>command-line client only. A [client] section may or may not be used for
>PHP connections. I had to send a "SET NAMES utf8" upon connection to
>really convince MySQL to use utf8 for the connection.


That's how it's supposed to be and was already mentioned multiple times.
How else should MySQL know what connection encoding the PHP client and
your scripts prefer?

>This really sucks, off course. It is plain stupid to have to set the
>encoding used encoded in the encoding it has to set. It is like sending
>a key INSIDE a safe which requires that key to open it.


You connect to the DB using the default encoding and then set it to
whatever you want it to be. There's nothing stupid about that.

Micha
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-07-2008, 08:39 AM
Juliette
 
Posts: n/a
Default Re: Mysql database in UTF8, PHP shows latin1 (iso-8859-1)

alex wrote:
> En/na Jerry Stuckle ha escrit:
>
>>
>> Check Micha's comment again:
>>
>> "...and PHP has to send a correct HTTP header back to the browser."
>>
>> The dataset connection and charset have nothing to do with the charset
>> sent in the page header. You can set the correct charset for the page
>> in your Apache server or in PHP.

>
>
> It's not a http header problem as i have in my script:
>
> <?php header("Content-type: text/html; charset=utf-8"); ?>
>
> it seems a php problem as when i execute "show variables" in an mysql
> console i get:
>
> character_set_client=utf8
>
> but from php i get:
>
> character_set_client=latin1
>
> when the default character set is utf8 (in my.cnf) and i have not
> configured it for latin1 anywhere.
>
> I've got 2 more apps one in java and the another one in .Net. None of
> those had to be modified in order to retrieve the results. But PHP needs
> the "set names utf8" in order to get the proper encoding.


Have a look here for a good explanation:
http://www.adviesenzo.nl/examples/ph...l_charset_fix/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:26 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com