选择语言 :

 Driver_Database_Driver_MySQLI::_connect

null Driver_Database_Driver_MySQLI::_connect( )
File: ./drivers/database/mysqli/mysqli.class.php
protected function _connect()
{
    $database = $hostname = $port = $socket = $username = $password = $persistent = null;
    extract($this->config['connection']);

    if (!$port>0)
    {
        $port = 3306;
    }

    # 检查下是否已经有连接连上去了
    if (Database_Driver_MySQLI::$_connection_instance)
    {
        if (is_array($hostname))
        {
            $hostconfig = $hostname[$this->_connection_type];
            if (!$hostconfig)
            {
                throw new Exception('指定的数据库连接主从配置中('.$this->_connection_type.')不存在,请检查配置');
            }
            if (!is_array($hostconfig))
            {
                $hostconfig = array($hostconfig);
            }
        }
        else
        {
            $hostconfig = array
            (
                $hostname
            );
        }

        # 先检查是否已经有相同的连接连上了数据库
        foreach ($hostconfig as $host)
        {
            $_connection_id = $this->_get_connection_hash($host, $port, $username);

            if (isset(Database_Driver_MySQLI::$_connection_instance[$_connection_id]))
            {
                $this->_connection_ids[$this->_connection_type] = $_connection_id;

                return;
            }
        }

    }

    # 错误服务器
    static $error_host = array();

    $last_error = null;
    while (true)
    {
        $hostname = $this->_get_rand_host($error_host);
        if (false===$hostname)
        {
            Core::debug()->error($error_host,'error_host');

            if ($last_error)throw $last_error;
            throw new Exception('connect mysqli server error.');
        }

        $_connection_id = $this->_get_connection_hash($hostname, $port, $username);
        Database_Driver_MySQLI::$_current_connection_id_to_hostname[$_connection_id] = $hostname.':'.$port;

        try
        {
            $time = microtime(true);

            $error_code = 0;
            $error_msg  = '';
            try
            {
                if (empty($persistent))
                {
                    $tmplink = mysqli_init();
                    mysqli_options($tmplink, MYSQLI_OPT_CONNECT_TIMEOUT, 3);
                    mysqli_real_connect($tmplink, $hostname, $username, $password, $database, $port, null, MYSQLI_CLIENT_COMPRESS);
                }
                else
                {
                    $tmplink = new mysqli($hostname, $username, $password, $database, $port);
                }
            }
            catch (Exception $e)
            {
                $error_msg  = $e->getMessage();
                $error_code = $e->getCode();
                $tmplink    = false;
            }

            if (false===$tmplink)
            {
                if (IS_DEBUG)throw $e;

                if (!($error_msg && 2===$error_code && preg_match('#(Unknown database|Access denied for user)#i', $error_msg)))
                {
                    $error_msg = 'connect mysqli server error.';
                }
                throw new Exception($error_msg, $error_code);
            }

            if (IS_DEBUG)Core::debug()->info('mysqli://'.$username.'@'.$hostname.':'.$port.'/'.$database.'/ connection time:' . (microtime(true) - $time));

            # 连接ID
            $this->_connection_ids[$this->_connection_type] = $_connection_id;
            Database_Driver_MySQLI::$_connection_instance[$_connection_id] = $tmplink;

            Database_Driver_MySQLI::$_current_databases[$_connection_id] = $database;

            unset($tmplink);

            break;
        }
        catch (Exception $e)
        {
            if (IS_DEBUG)
            {
                Core::debug()->error($username.'@'.$hostname.':'.$port.'.Msg:'.strip_tags($e->getMessage(), '') .'.Code:'. $e->getCode(), 'connect mysqli server error');
                $last_error = new Exception($e->getMessage(), $e->getCode());
            }
            else
            {
                $last_error = new Exception('connect mysqli server error', $e->getCode());
            }

            if (2===$e->getCode() && preg_match('#(Unknown database|Access denied for user)#i', $e->getMessage() , $m))
            {
                // 指定的库不存在,直接返回
                throw new Exception(strtolower($m[1])=='unknown database'?__('The mysql database does not exist'):__('The mysql database account or password error'));
            }
            else
            {
                if (!in_array($hostname, $error_host))
                {
                    $error_host[] = $hostname;
                }
            }
        }
    }
}