db_set_active() 訪問多個數據庫

若想訪問多個數據庫,首先需要在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(); //轉到默認數據庫
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章