This article mainly introduces the method of DRUPAL7 to connect multiple databases, the operation example, as well as the common problem solution, the need friend can refer to the following
If you encounter these problems: 1.Drupal How to connect to multiple databases? 2.Drupal connected to more than one database, but found that the program error, this is what? 3.Drupal get, add, modify, delete multiple databases, the data is not correctly written to the database or read to empty data, how to solve? 4. Just want to call or control another database in Drupal, but failed? Please take a look at the following introduction and how to solve your problem. How does Drupal connect to multiple databases? Allow Drupal to connect to multiple databases, you need to convert $db_url to an array. Default connection URL format for a single database (string): Copy code is as follows: $db _url = ' mysql://username:password@localhost/databasename '; $db _url = ' mysqli://username:password@localhost/databasename '; $db _url = ' pgsql://username:password@localhost/ DatabaseName '; supports multiple database URL formats (arrays): Code as follows: $db _url[' default ' = ' Mysql://drupal:drupal@localhost/drupal '; $db _url[' mydb '] = ' Mysql://user:pwd@localhost/anotherdb '; $db _url[' db3 '] = ' mysql://user:pwd@localhost/ Yetanotherdb '; When querying a different database, simply set the database through the $db_url reference key to the currently active database for use. Code as follows:<?php db_set_active (' mydb '); db_query (' SELECT * from Table_in_anotherdb '); // When data acquisition is complete, switch back to the default database connection. db_set_active (' default '); ?> This is the basic operation of Drupal's database operations. Two, Drupal connects to multiple databases, but sendsNow the program error, this is how? An error occurred while linking to multiple databases. The main possible causes are: 1. When you connect to another database, there is an error in SQL, which is an artificial code error, 2. When you connect the database, you can't find the data table in the other database, even if the SQL is correct, you should make an error. Solution: In the first case, according to the SQL error, to modify the SQL statement, the solution. In the second case, check to see if the database connection is crossed, meaning that you originally wanted to invoke the data table of another database, but the database connection has already been swapped elsewhere. For a database connection crossover, double-check the SQL statements that follow the Db_set_active function in the active database. III, does Drupal get, add, modify, delete multiple databases, not working correctly? 1, in Drupal SQL statements can be without data table prefix, only need to use braces {} contains a table can be in the database operation with the prefix of the data table. For example: Db_query (' SELECT * from {TABLE_IN_ANOTHERDB} '); But a database user, if you have multiple database permissions, you can do this directly on the current database connection without having to connect to the database in the $db_url setting. Set $db_prefix to implement cross database operations: Code is as follows: $db _prefix = Array ( ' default ' => ", ') Authmap ' => ' z_ ', ' profile_fields ' => ' usertable.z_ ', ' profile_values ' => ; ' Usertable.z_ ', ' users_roles ' => ' usertable.z_ ', ' users_fields ' => ' Usertable. ', ' role ' => ' usertable.z_ ', ' sessions ' => ' ' usertable.z_ ', 'Users ' => ' usertable.z_ ', ); The above code role, the current Drupal user information, such as all use usertable, so that multiple Drupal can share a user information database usertable, where z_ represents the data table prefix. NOTE: a). The Users table is used to present basic information about Drupal users, and can store all users ' common UID and its basic fields; B. Sessions tables are used to store Drupal user sessions and to count the online users of all sites c). The role table is used to store the roles of all Drupal stations; users_roles the permissions to store all Drupal stations; The above $db_prefix can be used to globally set the database to use that table, and the prefix for that table. This is just handy for using the standard {table} in Drupal's SQL statements. 2, if not through the $db_prefix to set, then the most straightforward way is to directly put the database table name in the SQL statement. For example: code as follows: Db_query ("Select uid from test.z_table1 WHERE name = '%s '", $name, MD5 ($pass)); The SQL statement above directly navigates to the test database, z_table the data table. So when you encounter Drupal getting, adding, modifying, and deleting multiple databases, the data is not correctly written to the database or read to the empty data, please make sure you control the database, datasheet location is correct. Four, in Drupal a function call or control other database See the following function framework code: Code as follows: Function test_fuc () { Global $db _url; Get global variables $db _url[' db_logs '] = ' mysqli://username:password@localhost/databasename '; db_set_ Active (' db_logs '); $codehere; SQL&NB to place Operations Db_logs database connections hereSp db_set_active (' Default '); } especially to the main, $db _url is a global variable, you need to use global reference in local functions: Copy code code as follows: Global $db _url; After getting the global variable set up the database, remember to use Db_set_active (' Default '), and set the database connection back to the default.