db_pdo::connect PHP Method

connect() public method

连接数据库方法
public connect ( $config = '', $linkNum )
    public function connect($config = '', $linkNum = 0)
    {
        if (!isset($this->linkID[$linkNum])) {
            if (empty($config)) {
                $config = $this->config;
            }
            if ($this->pconnect) {
                $config['params'][PDO::ATTR_PERSISTENT] = true;
            }
            if (version_compare(PHP_VERSION, '5.3.6', '<=')) {
                //禁用模拟预处理语句
                $config['params'][PDO::ATTR_EMULATE_PREPARES] = false;
            }
            //$config['params'][PDO::ATTR_CASE] = PDO::CASE_UPPER;
            if (!$config['dsn']) {
                $config['dsn'] = 'mysql:host=' . $config['hostname'] . ';dbname=' . $config['database'] . ';charset=' . $config['charset'];
            }
            try {
                $this->linkID[$linkNum] = new PDO($config['dsn'], $config['username'], $config['password'], $config['params']);
            } catch (\PDOException $e) {
                //E($e->getMessage());
                throw new Exception($e->getMessage());
            }
            // 因为PDO的连接切换可能导致数据库类型不同,因此重新获取下当前的数据库类型
            $this->dbType = $this->_getDsnType($config['dsn']);
            if (in_array($this->dbType, array('MSSQL', 'ORACLE', 'IBASE', 'OCI'))) {
                // 由于PDO对于以上的数据库支持不够完美,所以屏蔽了 如果仍然希望使用PDO 可以注释下面一行代码
                //E('由于目前PDO暂时不能完美支持'.$this->dbType.' 请使用官方的'.$this->dbType.'驱动');
            }
            $this->linkID[$linkNum]->exec('SET NAMES ' . $config['charset']);
        }
        return $this->linkID[$linkNum];
    }