#1 xieguolun
修改speed.php中的dbInstancepublic function dbInstance($db_config, $db_config_key, $force_replace = false){
if($force_replace || empty($GLOBALS['mysql_instances'][$db_config_key])){
try {
if(!class_exists("PDO") || !in_array("mysql",PDO::getAvailableDrivers(), true)){
err('Database Err: PDO or PDO_MYSQL doesn\'t exist!');
}
$GLOBALS['mysql_instances'][$db_config_key] = new PDO($db_config['dns'], $db_config['username'], $db_config['password'], $db_config['SQL_OPTION']);
}catch(PDOException $e){err('Database Err: '.$e->getMessage());}
}
return $GLOBALS['mysql_instances'][$db_config_key];
}
在配置文件中设置多个数据库:
"localhost" => array( // 调试配置
'debug' => 1,
'mysql' => array(
'dns' => 'mysql:host=localhost;dbname=test',
'username' => 'root',
'password' => 'usbw',
'CHARSET' => 'utf8',
'SQL_OPTION' => array(
PDO::ATTR_CASE => PDO::CASE_NATURAL,
//PDO::ATTR_PERSISTENT => true //长连接
),
),
'sqlite' => array(
'dns' => 'sqlite:d:/wlsj.db',
'username' => 'root',
'password' => 'usbw',
'CHARSET' => 'utf8',
'SQL_OPTION' => array(
PDO::ATTR_CASE => PDO::CASE_NATURAL,
//PDO::ATTR_PERSISTENT => true //长连接
PDO::FETCH_ASSOC
),
),
),
在控制器中使用:
function actionA(){
$a=new Model();
// $a = new User();
$b=$a->dbInstance($GLOBALS['sqlite'], 'master', true);
$sql="select * from SWJG";
$c=$b->query($sql);
foreach ($c as $row) {
print_r($row['SWJGMC']);
}
}
这样修改后就支持多个数据库,并且支持不同的数据库,只是除mysql以后只能用原生的查询语句。
2020-04-25 15:48:05