Example #1
0
 protected static function init_mysql($is_read = true, $is_master = false)
 {
     self::$dataconfig = $GLOBALS['database'];
     //选择数据库配置
     if (empty(self::${$link})) {
         try {
             $link = 'link_read';
             self::${$link} = mysql_connect(self::$dataconfig['db_host'], self::$dataconfig['db_user'], self::$dataconfig['db_pass']);
             if (empty(self::${$link})) {
                 throw new Exception(mysql_error(), 10);
             } else {
                 if (mysql_get_server_info() > '4.1') {
                     $charset = str_replace('-', '', strtolower(self::$dataconfig['db_charset']));
                     mysql_query("SET character_set_connection=" . $charset . ", character_set_results=" . $charset . ", character_set_client=binary");
                 }
                 if (mysql_get_server_info() > '5.0') {
                     mysql_query("SET sql_mode=''");
                 }
                 if (@mysql_select_db(self::$dataconfig['db_name']) === false) {
                     throw new Exception(mysql_error(), 11);
                 }
             }
         } catch (Exception $e) {
             if (TRUE) {
                 if ($e->getCode() == 10) {
                     echo '数据库连接失败,可能是数据库服务器地址、账号或密码错误';
                 } elseif ($e->getCode() == 11) {
                     echo '数据库' . self::$dataconfig['db_name'] . '不存在';
                 } else {
                     echo 'Can\'t connect to MySQL server';
                 }
             } else {
                 echo $e->getMessage(), '<br/>', '<pre>', $e->getTraceAsString(), '</pre>';
             }
             self::log($e->getMessage(), '初始化');
             exit;
         }
     }
     return self::${$link};
 }