--- Wakka.class.php.orig 2008-02-17 01:24:58.000000000 -0800 +++ Wakka.class.php 2008-02-21 03:34:13.000000000 -0800 @@ -56,6 +56,9 @@ $this->dblink = @mysql_connect($this->config["mysql_host"], $this->config["mysql_user"], $this->config["mysql_password"]); if ($this->dblink) { + $charset = $this->_getDbCharset($this->config["mysql_database"]); + if($charset) + @mysql_query("SET NAMES '$charset'", $this->dblink); if (!@mysql_select_db($this->config["mysql_database"], $this->dblink)) { @mysql_close($this->dblink); @@ -64,6 +67,25 @@ } $this->VERSION = WAKKA_VERSION; } + function _getDbCharset($db) { + if (!$db) { + return false; + } + + // We use a SHOW CREATE DATABASE command to show the original + // SQL character set when DB was created. + $result = mysql_query("SHOW CREATE DATABASE `$db`", $this->dblink); + if (mysql_num_rows($result) < 0 ) { + // The specified db name is wrong! + return false; + } + $dbInfo = mysql_fetch_row($result); + $pattern = '/40100 DEFAULT CHARACTER SET (\w+) /'; + if ( (preg_match($pattern, $dbInfo[1], $match) > 0) ) { + return $match[1]; + } + return false; + } /** * Database methods