--- a/nucleus/libs/sql/mysql.php 2009-09-19 20:03:20.556432000 -0700 +++ b/nucleus/libs/sql/mysql.php 2009-09-19 20:03:15.274238000 -0700 @@ -61,12 +61,38 @@ global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN; $MYSQL_CONN = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD) or startUpError('

Could not connect to MySQL database.

', 'Connect Error'); + $charset = _getDbCharset($MYSQL_DATABASE); + if($charset) + mysql_query("SET NAMES '$charset'"); mysql_select_db($MYSQL_DATABASE) or startUpError('

Could not select database: ' . mysql_error() . '

', 'Connect Error'); return $MYSQL_CONN; } /** + * Get DB default charset + */ + 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`"); + 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; + } + + /** * disconnects from SQL server */ function sql_disconnect($conn = false) {