Cml\Db\MySql\Pdo::connect PHP Method

connect() public method

Db连接
public connect ( string $host, string $username, string $password, string $dbName, string $charset = 'utf8', string $engine = '', boolean $pConnect = false ) : mixed
$host string 数据库host
$username string 数据库用户名
$password string 数据库密码
$dbName string 数据库名
$charset string 字符集
$engine string 引擎
$pConnect boolean 是否为长连接
return mixed
    public function connect($host, $username, $password, $dbName, $charset = 'utf8', $engine = '', $pConnect = false)
    {
        $link = '';
        try {
            $host = explode(':', $host);
            $dsn = "mysql:host={$host[0]};" . (isset($host[1]) ? "port={$host[1]};" : '') . "dbname={$dbName}";
            if ($pConnect) {
                $link = new \PDO($dsn, $username, $password, [\PDO::ATTR_PERSISTENT => true, \PDO::ATTR_EMULATE_PREPARES => false]);
            } else {
                $link = new \PDO($dsn, $username, $password, [\PDO::ATTR_EMULATE_PREPARES => false]);
            }
        } catch (\PDOException $e) {
            throw new PdoConnectException('Pdo Connect Error! {' . $host[0] . (isset($host[1]) ? ':' . $host[1] : '') . ', ' . $dbName . '} Code:' . $e->getCode() . ', ErrorInfo!:' . $e->getMessage() . '<br />', 0, $e);
        }
        $link->exec("SET names {$charset}");
        //$link->exec('set sql_mode="";'); 放数据库配 特殊情况才开
        if (!empty($engine) && $engine == 'InnoDB') {
            $link->exec('SET innodb_flush_log_at_trx_commit=2');
        }
        return $link;
    }