Page 1 of 1
PHP MySQL UTF8
Posted: Wed Sep 02, 2009 1:17 pm
by tuga
I have this problem I'm building a site and I want to use the encoding of UTF-8, so in the header of my html pages I have
Code: Select all
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
I'm getting some data from a MySQL database that has the Character set utf8 (UTF-8 Unicode) and the Collation utf8_general_ci.
When I echo the data in the page I get this small squares filled up with letters( ? ) when there are some special characters like รง.
If I do something like
Code: Select all
echo htmlentities($row['str'], ENT_COMPAT, 'ISO-8859-1');
it produces the correct result.
What I would like is to understand why this happens if everything is set to utf-8 and why do I have to convert to latin1 to make it work.
Thanks
Re: PHP MySQL UTF8
Posted: Wed Sep 02, 2009 1:48 pm
by Eran
Probably the database connection collation is ISO-8859 (for some reason, this is the default in many distributions). You can change in two ways - edit the mysql configuration file, or issue a names / charset query
http://dev.mysql.com/doc/refman/5.0/en/ ... ction.html
Re: PHP MySQL UTF8
Posted: Wed Sep 02, 2009 1:50 pm
by jazz090
all settings (character set and collation) in mysql need to be utf8_unicode_ci not general and certainly not latin. you have to ensure that both the submitting page and the printing page contain the meta tag header. if you upload to db by JUST mysql_real_escape_string and echo it back, it should work properly without htmlentities.
Re: PHP MySQL UTF8
Posted: Wed Sep 02, 2009 1:59 pm
by Eran
all settings (character set and collation) in mysql need to be utf8_unicode_ci not general
That's certainly not a requirement for using UTF. the difference between the various flavors of the same set (utf_*) is accuracy and speed of comparison and sorting. All of those can be used to store UTF content.
Re: PHP MySQL UTF8
Posted: Wed Sep 02, 2009 2:23 pm
by tuga
pytrin wrote:Probably the database connection collation is ISO-8859 (for some reason, this is the default in many distributions). You can change in two ways - edit the mysql configuration file, or issue a names / charset query
http://dev.mysql.com/doc/refman/5.0/en/ ... ction.html
You are completely right!!!
I tried both solutions and everything works fine.
I have just installed the MySQL Server on my PC and on the Options I have set it to utf8, and there is actually a line in my.ini with this option
default-character-set=utf8
do you have any ideas why doesn't take that in consideration, and I have to add
init_connect= 'SET NAMES utf8'
Re: PHP MySQL UTF8
Posted: Wed Sep 02, 2009 2:26 pm
by Eran
A somewhat annoying and little documented feature of mysql is that the client can override the default charset. add the following directive to your mysql configuration file -
Code: Select all
skip-character-set-client-handshake