我是CodeIgniter的新手,我正在构建一个项目,我需要访问大约40个数据库。
现在将它们全部添加到配置文件中似乎分配了一些无用的工作,将来还会添加更多数据库。
有没有办法在不使用数据库配置设置的情况下访问codeigniter框架中的数据库?
提前致谢!
问候,
Beakerv
答案 0 :(得分:0)
是的,你可以实现你想要的目标
class Your_Model extends CI_Model
{
protected $customDb;
public function __construct()
{
parent::__construct();
$this->loadCustomerDatabase();
}
private function loadCustomerDatabase()
{
//define your customers database
$config['hostname'] = 'localhost';
$config['username'] = 'myusername';
$config['password'] = 'mypassword';
$config['database'] = 'your_db';
$config['dbdriver'] = 'pdo';
$config['dbprefix'] = '';
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = '';
$config['char_set'] = 'utf8';
$config['dbcollat'] = 'utf8_general_ci';
$this->customDb = $this->load->database($config, true);
//you can use this instance like any other db instance
$objQuery = $this->customDb->select("*")->from("your_table")->get();
}
}