不过,用习惯了DSN真的很方便(我的项目中需要连接好多个不同的数据库),现在突然不能用了,除了自己重写parse_url功能,有没有别的解决办法?
对应代码在DB.php中:
/**
* DSN 解析
* 格式: mysql://username:[email protected]:3306/DbName?param1=val1¶m2=val2#utf8
* @access private
* @param string $dsnStr 数据库 DSN 字符串解析
* @return array
*/
private static function parseDsn($dsnStr)
{
$info = parse_url($dsnStr);
if (!$info) {
return [];
}
$dsn = [
'type' => $info['scheme'],
'username' => isset($info['user']) ? $info['user'] : '',
'password' => isset($info['pass']) ? $info['pass'] : '',
'hostname' => isset($info['host']) ? $info['host'] : '',
'hostport' => isset($info['port']) ? $info['port'] : '',
'database' => !empty($info['path']) ? ltrim($info['path'], '/') : '',
'charset' => isset($info['fragment']) ? $info['fragment'] : 'utf8',
];
if (isset($info['query'])) {
parse_str($info['query'], $dsn['params']);
} else {
$dsn['params'] = [];
}
return $dsn;
}
最佳答案
