若想訪問多個數據庫,首先需要在settings.php中,配置好多個數據庫:
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'tianjinyizhuan',
'username' => 'tianjinyizhuan',
'password' => 'tianjinyizhuan',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
'commons' =>
array (
'default' =>
array (
'database' => 'commons',
'username' => 'commons',
'password' => 'commons',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);
然後,在代碼中訪問別的數據庫:
db_set_active('commons'); // 切換到commons定義的數據庫
// 操作數據庫commons
db_set_active(); //轉到默認數據庫