How to make Horde connect to mysql with UTF-8 character set?

Posted by jkj on Server Fault See other posts from Server Fault or by jkj
Published on 2011-06-30T12:51:48Z Indexed on 2012/09/03 21:40 UTC
Read the original article Hit count: 371

Filed under:
|
|
|

How to tell horde 3.3.11 to use UTF-8 for it's mysql connection? The $conf['sql']['charset'] only tells horde what is expected from the database.

Horde uses MDB2 to connect to mysql. Is there way to force MDB2 or mysql character_set_client from php.ini?

So far I found two workarounds:

  • Force mysql to ignore character set requested by client

    [mysqld]
    skip-character-set-client-handshake=1
    default-character-set=utf8
    
  • Force mysql to run SET NAMES utf8 on every connection

    [mysqld]
    init-connect='SET NAMES utf8'
    

Both have drawbacks on multi user mysql server. The first disables converting character sets alltogether and the second one forces every connection to produce UTF-8.

[EDIT]

Found the problem. The 'charset' parameter was unset the last minute before sending to SQL backend. This is probably due to mysql not being able to digest utf-8 but utf8. Mysql specific mapping is required to make it work.

I just worked around it by translating utf-8 -> utf8. Won't work with any other databases with this patch though.

--- lib/Horde/Share/sql.php.orig    2011-07-04 17:09:33.349334890 +0300
+++ lib/Horde/Share/sql.php 2011-07-04 17:11:06.238636462 +0300
@@ -753,7 +753,13 @@
         /* Connect to the sql server using the supplied parameters. */
         require_once 'MDB2.php';
         $params = $this->_params;
-        unset($params['charset']);
+
+        if ($params['charset'] == 'utf-8') {
+            $params['charset'] = 'utf8';
+        } else {
+            unset($params['charset']);
+        }
+
         $this->_write_db = &MDB2::factory($params);
         if (is_a($this->_write_db, 'PEAR_Error')) {
             Horde::fatal($this->_write_db, __FILE__, __LINE__);
@@ -792,7 +798,13 @@
         /* Check if we need to set up the read DB connection seperately. */
         if (!empty($this->_params['splitread'])) {
             $params = array_merge($params, $this->_params['read']);
-            unset($params['charset']);
+
+            if ($params['charset'] == 'utf-8') {
+                $params['charset'] = 'utf8';
+            } else {
+                unset($params['charset']);
+            }
+
             $this->_db = &MDB2::singleton($params);
             if (is_a($this->_db, 'PEAR_Error')) {
                 Horde::fatal($this->_db, __FILE__, __LINE__);

© Server Fault or respective owner

Related posts about mysql

Related posts about pear