之前有提及,Drupal支持比较常见的数据库种类,但其实不仅仅如此,在Drupal开发过程中,可能遇到跨数据库操作的情况,比如,从学生数据库中读取学生信息,Drupal还可以同时支持多数据库,Drupal多数据库操作如下:
1、配置Drupal多数据库,只需要配置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’ , ), ), ‘legacy’ => array( ‘default’ => array( ‘driver’ => ‘mysql’ , ‘database’ => ‘legacydatabasename’ , ‘username’ => ‘legacyusername’ , ‘password’ => ‘legacypassword’ , ‘host’ => ‘legacy’ , ‘port’ => 6060, ‘prefix’ => ‘legacyprefix_’ , ‘collation’ => ‘utf8_general_ci’ , ), ), );
2、在Drupal模块开发中,切换主\备数据库
//从其他数据库查询信息 db_set_active(‘legacy’ ); $result = db_query(“select * from table where uid = :uid” , array(‘:uid’ => $user->uid)); //切换到默认数据库 db_set_active(‘default’ )