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 (string) for a single database:
Copy Code code as follows:
$db _url = ' mysql://username:password@localhost/databasename ';
$db _url = ' mysqli://username:password@localhost/databasename ';
$db _url = ' pgsql://username:password@localhost/databasename ';
URL format (array) that supports multiple databases:
Copy Code 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.
Copy Code 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 operation.
Second, Drupal connected to multiple databases, but found that the program error, this is how?
An error occurs when linking to multiple databases, mainly because of the following reasons:
1. When connecting to another database, SQL error, this is artificial code error;
2. Connect the database when cross, so in other databases can not find the data table, even if the SQL is correct, also want to error;
Workaround:
For the first case, please modify the SQL statement according to the SQL error, and then solve the problem.
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.
When Drupal gets, adds, modifies, deletes multiple databases, does it not work 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 up $db_prefix to implement Cross-database operations:
Copy Code code 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, the current Drupal user information, such as all the use of usertable, so that multiple Drupal can share a user information database usertable, where z_ represents the data table prefix.
Attention:
A). The Users table is used to present basic information about Drupal users, and can store all user-shared UID and its basic fields;
b). The sessions table is used to store Drupal user sessions, which can count the online users of all sites;
c). The role table is used to store the roles of all Drupal stations;
d). Users_roles the right to store all Drupal stations;
The above $db_prefix allows you to globally set the database to use for that table, and the prefix for that table, which 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 directly to the database table name in the SQL statement.
For example:
Copy Code 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
Consider the following function framework code:
Copy 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; Here, you place the SQL that operates the Db_logs database connection
Db_set_active (' Default ');
}
In particular, $db _url is a global variable and needs to be referenced in local functions using global references:
Copy Code code as follows:
Global $db _url; Get Global variables
When you are finished setting up the database, remember to use Db_set_active (' Default '), and set the database connection back to the default.