Drupal7链接到多个数据库

来源:互联网 发布:ios 广告拦截 知乎 编辑:程序博客网 时间:2024/05/17 22:11

n Drupal 7, first construct a database connection array, then connect to it to Drupal's front-controller, then execute queries using D7's database abstraction layer.

<?php
  $other_database
= array(
     
'database' => 'databasename',
     
'username' => 'username', // assuming this is necessary
     
'password' => 'password', // assuming this is necessary
     
'host' => 'localhost', // assumes localhost
     
'driver' => 'mysql', // replace with your database driver
 
);
 
// replace 'YourDatabaseKey' with something that's unique to your module
 
Database::addConnectionInfo('YourDatabaseKey', 'default', $other_database);
 
db_set_active('YourDatabaseKey');

 
// execute queries here

 
db_set_active(); // without the paramater means set back to the default for the site
 
drupal_set_message(t('The queries have been made.'));
?>

 

 

原创粉丝点击