Gentoo: MySQL and PHP Charset Problems

At the mo­ment, Gen­too is ex­pe­ri­enc­ing sev­eral in­con­sis­tency and prob­lems with char­ac­ter sets be­tween MySQL and PHP. This is pri­marely based on MySQL-4.1 now up­dat­ing from 4.0 with­out warn­ing and user in­ter­ac­tion which most of the times breaks ex­ist­ing ex­tended char­ac­ters as MySQL now stores every dump from for­mer data­bases as UTF-8, which is still badly sup­ported by PHP.

For many PHP web ap­pli­ca­tions which ex­pe­ri­ence prob­lems with ex­tended char­ac­ters (like um­lauts, ac­cents, …), the fol­low­ing hack might help.

  1. Lo­cate the file where the mysql data­base con­nec­tion is opened.
  2. Add the fol­low­ing com­mands after open­ing the data­base con­nec­tion:
    mysql_query('SET character_set_client=latin1');
    mysql_query('SET character_set_results=latin1');
    mysql_query('SET character_set_connection=latin1');

This will re­sume using lat­in1 in­stead of UTF-8 for the con­nec­tion and the re­sult set. For per­for­mance rea­sons, the data in the data­base should then be stored as lat­in1 as well.

Ac­cord­ing to mes­sages in the Gen­too Forum, the de­vel­op­ers have now re­leased an ebuild for PHP (both 5.x and 4.4.2) that will re­gard char­ac­ter-set set­tings in my.​cnf in a sec­tion es­pe­cially for php (still in un­sta­ble). You should use the sec­tion [php-cli], [php-cgi] and/or [php-apache2han­dler]. Un­for­tu­nately I have not yet had time to test this out.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.