Binary Expressions

2007-11-16

French Characters Not Rendering Correctly

Filed under: — The Warden @ 9:13 am

Background Knowledge


The MySQL database v4.0.23 is using the default character set of “Latin1″. When the database was created I had no knowledge of character sets other wise it would have been “UTF-8″.
The web pages are using a character set of “UTF-8″.

Problem


Data being queried from a MySQL database that contains French accent characters will not render correctly in the browser even after applying PHP htmlentities().

Example code: $string = htmlentities($string , ENT_QUOTES, “UTF-8″);

Solution


The queried data from the database was inputted using the character set “ISO-8859-1″. I found this out by changing the browser’s character encoding to “Western ISO-8859-1″ and the French accent characters then rendered properly. With the use of PHP iconv() I was able to convert the data from “ISO-8859-1″ to “UTF-8″ character set and the French characters then rendered properly in the browser.

Example code: $string = iconv(”ISO-8859-1″,”UTF-8″,”$string”);


If you are unaware of what MySQL default character set being used, you can run this SQL command “show variables like “%character%” and check your MySQL configuration file. Refer to the MySQL manual for further details.

4 Responses to “French Characters Not Rendering Correctly”

  1. mike Says:

    what i’ve realized is that no matter what column type (latin1, utf-8) in mysql, if the browser’s meta tag is set to utf-8, it will encode it on input and on output.

    of course, if you wanted to do anything specific with the data outside of a browser, you’d probably run in to trouble though. but otherwise mysql appears to treat it just like normal binary data and there’s no special treatment needed in PHP or MySQL…

  2. Mark Robson Says:

    You would probably find it easier, and it would be more correct, to set the database’s client connection encoding to utf8 instead of using iconv() in PHP.

    Just do “SET NAMES utf8″ after connecting each time.

    Mark

  3. The Warden Says:

    Re: Mike

    Yes I would agree it does do that and rightfully it should as you have specified specifically to use a character encoding set in the meta tags.

  4. The Warden Says:

    Re: Mark

    I would entirely agree Mark. However I’m using MySQL v4.0.23 and that statement will not work. My assumptions are that proper support for character encoding was not supported until v4.1. I was unable to find anyone to help me further on this with the version I have installed. I would definitely upgrade if I could.

Leave a Reply

Take back your mailbox - CAUCE.org

Powered By Wordpress PHP: Hypertext Preprocessor MySQL Powered Download Juice, the cross-platform podcast receiver
Proud To Be Canadian Get Firefox Valid XHTML Valid CSS
<NO>OOXML Logo


24 queries. 0.266 seconds.
Copyright © 2004 - 2005 by Adam Douglas