1
OeilNoir
Mysql5 and UTF-8 wrong encoding...
  • 2007/11/3 12:04

  • OeilNoir

  • Just popping in

  • Posts: 5

  • Since: 2007/2/20


I used Mysql4 for quite a while now and since Mysql5 is availlable at my provider, I want to upgrade to that. I know my way around MySQL a bit so I had no problem changing the settings in XOOPS to point to the right database and to insert everything into that new database via PhpMyAdmin.

Now my problem is this: everything in my database is in UTF-8 (I made sure of that since the first day I used the database in mysql4). I checked everything and I am viewing phpmyadmin in UTF-8, phpmyadmin is in UTF-8 and I see the data correctly (I use japanese and french on my site and all characters are fine). I made sure that XOOPS also is encoded in UTF-8. The browser is in that mode and the languages setting point to UTF-8 (and where set a long time ago when I first fired up xoops).

The data displayed on my browser displays bad characters. Wich can only mean that there is something wrong between XOOPS and the database (they are not in sink). I made sure of this by forcing the encoding of the transfered data with a "SET NAMES UTF8" function and then the data displays fine BUT once I log-off, I cannot login on my site anymore and can't navigate around.

If I don't do the "SET NAMES" function, All the characters I enter in the browser display fine but when I look in the database, they become corrupted. If I force the "SET NAMES" then enter the same characters again, they display fine in the browser and in the database.

My problem is I don't know the protocols to call data to and from the database in PHP so theres something I did wrong with that "SET NAMES" function.

Can somebody help me?

2
OeilNoir
Re: Mysql5 and UTF-8 wrong encoding...
  • 2008/2/17 9:51

  • OeilNoir

  • Just popping in

  • Posts: 5

  • Since: 2007/2/20


After A LOT of searching around, I finally found the answer and I'll post it so that anybody encountering the same situation doesn't have to go through the same ordeal as me.

It seams that trough a problem of some sort (probably centered in the php version used), the encoding and setnames function where not in sinc. The following should be a standard hack on future XOOPS versions if they want to claim that it is pure UTF-8 for it will force most systems to the UTF-8 encoding (my provider didn't let me access the php settings). The first thing, of course is to make sure the global.php file in your languages folder points to UTF-8 and not another encoding. It should be done for all your languages and you'll find that encoding near the end of the file.

Then, go to your Xoops/class/database/ folder and edit the file: mysqldatabase.php

In the "connect" function, right before the "return true" part, insert those 3 lines.
mysql_query('SET character_set_results="utf8"');
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");

Your all set to go.
Try all the languages in the world that you want and then go see in your database with mysqladmin and you'll see that no weird characters are there.

On a fresh installation, people may think that their XOOPS is trully UTF-8 since all the characters display fine but if you take a look at the database itself, you may notice that the encoding is wrongly passed. I noticed it because I had text from a previous installation that was right in the database, hence displayed wrongly in the browser. This will cause big headackes when you upgrade your database or just want to edit stuff directly into it. Friendly warning.

3
avtx30
Re: Mysql5 and UTF-8 wrong encoding...
  • 2008/2/21 17:47

  • avtx30

  • Not too shy to talk

  • Posts: 181

  • Since: 2006/10/12


Thank you for sharing this.

I've just solved the problem in a different way. Instead of touching XOOPS codes, I changed the MySQL settings.

I did as following:

# ssh_to_my_remote_server
# mysqldump -u mysql_username -p --default-character-set=latin1 old_db > latin1_db.sql
# sed 's/latin1/utf8/g' latin1_db.sql > utf8_db.sql
# MySQL -u mysql_username -p new_db < utf8_db.sql


Change the config file of MySQL (my.cnf) so that it looks like this:
[mysqld]
default-
character-set=utf8
skip
-character-set-client-handshake


The most important thing here is the above 2 lines: one to force default character set in MySQL to "utf8", another is to tell MySQL that "do not automatically convert character sets between server and client"

Restart the MySQL server. Bingo!! The characters are displayed properly both in phpmyadmin and in xoops.

Hope this help.

Login

Who's Online

351 user(s) are online (283 user(s) are browsing Support Forums)


Members: 0


Guests: 351


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits