From a3ec1faf0240616c47aa5a7d3d321e5b1dd02ae2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 21 Jan 2016 14:42:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Db=E7=B1=BB=E7=9A=84parseDsn?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Db.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/library/think/Db.php b/library/think/Db.php index 45debb84..ca97faf5 100644 --- a/library/think/Db.php +++ b/library/think/Db.php @@ -39,6 +39,9 @@ class Db if (!isset(self::$instances[$md5])) { // 解析连接参数 支持数组和字符串 $options = self::parseConfig($config); + if (empty($options['type'])) { + throw new Exception('db type error'); + } // 如果采用lite方式 仅支持原生SQL 包括query和execute方法 $class = $lite ? '\\think\\db\\Lite' : (!empty($options['namespace']) ? $options['namespace'] : '\\think\\db\\driver\\') . ucwords($options['type']); self::$instances[$md5] = new $class($options); @@ -80,10 +83,9 @@ class Db */ private static function parseDsn($dsnStr) { - if (empty($dsnStr)) {return false;} $info = parse_url($dsnStr); if (!$info) { - return false; + return []; } $dsn = [ 'type' => $info['scheme'], @@ -91,7 +93,7 @@ class Db 'password' => isset($info['pass']) ? $info['pass'] : '', 'hostname' => isset($info['host']) ? $info['host'] : '', 'hostport' => isset($info['port']) ? $info['port'] : '', - 'database' => isset($info['path']) ? substr($info['path'], 1) : '', + 'database' => !empty($info['path']) ? ltrim($info['path'], '/') : '', 'charset' => isset($info['fragment']) ? $info['fragment'] : 'utf8', ];