If you encounter these problems:
1. How does Drupal connect to multiple databases?
2. After Drupal connects to multiple databases, it finds that the program reports an error. Why?
3. When Drupal acquires, adds, modifies, or deletes multiple databases, data is not correctly written to the database or empty data is read. How can this problem be solved?
4. I only want to call a function in Drupal or control other databases, but it fails?
Please take a closer look at the introduction and how to solve your problem.
1. How does Drupal connect to multiple databases?
To allow Drupal to connect to multiple databases, You need to convert $ db_url to an array.
By default, the URL format (string) for connecting to a single database is as follows ):
Copy codeThe 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 ';
The URL format (array) of multiple databases is supported ):
Copy codeThe Code is 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, you can simply set the database to the active database through the reference key of $ db_url.
Copy codeThe Code is as follows: <? Php
Db_set_active ('mydb ');
Db_query ('select * FROM table_in_anotherdb ');
// After the data is obtained, switch back to the default database connection.
Db_set_active ('default ');
?>
This is the basic operation for Drupal database operations.
2. After Drupal connects to multiple databases, it finds that the program reports an error. Why?
When connecting to multiple databases, an error is reported, which may be due to the following reasons:
1. An SQL error occurred while connecting to other databases. This is a human code error;
2. When the database is connected, an error is reported even if the SQL statement is correct because the data table cannot be found in other databases;
Solution:
In the first case, modify the SQL statement based on the SQL error.
In the second case, check whether the database connection is crossed. This means that the data table of another database is called, but the database connection has been changed to another place. For database connection crossover, check whether the SQL statement after the function db_set_active is in the active database.
Iii. Does Drupal not work properly when obtaining, adding, modifying, or deleting multiple databases?
1. In Drupal, SQL statements do not contain the data table prefix. You only need to use braces {} to include the table to add the data table prefix during database operations.
Example: db_query ('select * FROM {table_in_anotherdb }');
However, if a database user has permissions for multiple databases, you do not need to set $ db_url to connect to the database and directly operate on the current database connection.
Set $ db_prefix to perform cross-database operations:
Copy codeThe 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 _',
'Session' => 'usertable. z _',
'Users' => 'usertable. z _',
);
When the code above Acts, the current Drupal user and other information 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 store the basic information of Drupal users and the UID and basic fields shared by all users;
B) The sessions table is used to store Drupal user Sessions, which can count the number of online users of all sites;
C) The role table is used to store roles of all Drupal sites;
D). users_roles: Permission to store all Drupal sites;
Through the above $ db_prefix, You can globally set the database to be used for that table and the prefix of that table. This is just to facilitate the use of standard {table} In Drupal SQL statements }.
2. If you do not use $ db_prefix, the straightforward method is to directly store the database table name in the SQL statement.
For example:
Copy codeThe Code is as follows:
Db_query ("SELECT uid FROM test. z_table1 WHERE name = '% s' and pass =' % S'", $ name, md5 ($ pass ));
The preceding SQL statement is directly located in the test database and z_table data table.
Therefore, when Drupal acquires, adds, modifies, or deletes multiple databases, the data is not correctly written to the database or empty data is read, check whether the database and data table you control are in the correct position.
4. Call a function in Drupal or control other databases
See the following function framework code:
Copy codeThe Code is as follows:
Function test_fuc (){
Global $ db_url; // obtain global variables
$ Db_url ['db _ logs'] = 'mysqli: // username: password @ localhost/databasename ';
Db_set_active ('db _ logs ');
$ Codehere; // The SQL statement used to operate the db_logs database connection
Db_set_active ('default ');
}
In particular, $ db_url is a global variable and must be referenced by global in a local function:
Copy codeThe Code is as follows: global $ db_url; // obtain the global variable
After setting the database, remember to use db_set_active ('default'); and set the database connection to restore to the default value.