Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL using Latin1(iso-8859-1) despite UTF-8 settings

Once again I have a weird and tricky problem.

I've been working with converting my MySQL databases (and everything else on my server for that matter) to UTF-8 to avoid having to convert text when getting and putting text into the different databases.

I think I've partially succeeded because SHOW VARIABLES LIKE 'character_set%' returns:

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

But still mysql_client_encoding($con); returns latin1 and in the output every special character is replaced with �. My conclusion is that the client or the connection between PHP and the MySQL database is using latin1 even though I've been specifying utf-8 in the document header and in my.ini with the following code:

character-set-server = utf8
character-set-client = utf8
default-character-set = utf8

edit: I've added the settings above under both [client], [mysqld] and [mysql]

When I use mysql_query('SET NAMES utf8;'); or mysql_set_charset("utf8"); the text shows properly but for me that's not a sollution, just a temporary fix.

Does anyone know how to force PHP (or whatever it is reverting to latin1) to use utf-8?

I should mention that I'm using Windows 2003 Server and Apache 2.

like image 359
user1571510 Avatar asked Aug 30 '12 19:08

user1571510


People also ask

How to set UTF-8 encoding in PHP?

PHP UTF-8 Encoding – modifications to your php. The first thing you need to do is to modify your php. ini file to use UTF-8 as the default character set: default_charset = "utf-8"; (Note: You can subsequently use phpinfo() to verify that this has been set properly.)

How do I set MySQL database to UTF-8?

To change the character set encoding to UTF-8 for the database itself, type the following command at the mysql> prompt. Replace dbname with the database name: Copy ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci; To exit the mysql program, type \q at the mysql> prompt.

How do I change MySQL from UTF-8 to latin1?

Similarly, here's the command to change character set of MySQL table from latin1 to UTF8. Replace table_name with your database table name. mysql> ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; Hopefully, the above tutorial will help you change database character set to utf8mb4 (UTF-8).


1 Answers

I've been working with converting my MySQL databases (and everything else on my server for that matter) to UTF-8 to avoid having to convert text when getting and putting text into the different databases.

You would still have to make sure the target database (or system) is setup to use UTF-8 by default - and the library you are using to connect to is is using the correct character set. Just because data is in UTF-8 doesn't make it universally compatible.

When I use mysql_query('SET NAMES utf8;'); or mysql_set_charset("utf8"); the text shows properly but for me that's not a sollution, just a temporary fix.

This is the proper solution. Since your connection's encoding determines how the data is received by your client (in this case, PHP).

To set it as default; you need to configure MySQL appropriately and then restart the server:

[client]
default-character-set=UTF8

Note that this affects all clients even the command line mysql client.

like image 97
Burhan Khalid Avatar answered Nov 09 '22 06:11

Burhan Khalid