Drupal7设置多个数据库连接

1.修改settings.php文件,添加数据库信息。

$databases = array( 
   'default' => 
   array( 
     'default' => 
     array( 
       'driver' => 'mysql', 
       'database' => 'databasename', 
       'username' => 'username', 
       'password' => 'password', 
       'host' => 'localhost', 
       'port' => 3306, 
       'prefix' => 'myprefix_', 
       'collation' => 'utf8_general_ci', 
     ), 
   ), 
'other' => 
   array( 
     'default' => 
     array( 
       'driver' => 'mysql', 
       'database' => '数据库', 
       'username' => '用户名', 
       'password' => '密码', 
       'host' => '数据库地址', 
       'port' => '端口', 
       'prefix' => '数据库前缀名', 
       'collation' => 'utf8_general_ci', 
     ), 
   ), 
); 

 

2、在模块中数据库的切换

<?php
/**
* Implementation of hook_menu().
*/
function test_menu() {
 $items['test'] = array(
 'title' => 'This is title',
 'page callback' => 'test_hello',
 'access callback' => TRUE,
 'type' => MENU_CALLBACK,
 );
 return $items;
} 

/**
* Page callback.
*/
function test_hello() {
 //从其他数据库查询信息
 db_set_active('main');
 $result = db_query("select * from node where nid = :nid", array(':nid' =>'170')); 

foreach($result as $res){ 

print_r($res->title); 

}

 $node = node_load('190');
 dsm($node);
 
 //切换到默认数据库
 db_set_active('default');
 
 return '';
} 

 

你可能感兴趣的:(drupal)