Drupal7 connects to multiple databases and frequently asked questions

  • 2021-01-18 06:20:32
  • OfStack

If you have any of these problems:
1. How does Drupal connect to multiple databases?
2.Drupal connection to multiple databases, but found a program error, this is what happened?
3.Drupal access, add, modify, delete multiple databases, data is not correctly written to the database or read empty data, how to solve?
4. Want to call a function or control other databases in Drupal, but failed?
Please take a close look at the following introduction and how to solve your problem.
1. How does ES11en connect to multiple databases?
To allow Drupal to connect to multiple databases, $db_url is converted to an array.
Default connection to a single database in URL format (string) :

$db_url = 'mysql://username:password@localhost/databasename'; 
$db_url = 'mysqli://username:password@localhost/databasename'; 
$db_url = 'pgsql://username:password@localhost/databasename';

Support URL format (array) for multiple databases:

$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 to the current active database with the reference key of $db_url.

<?php 
db_set_active('mydb'); 
db_query('SELECT * FROM table_in_anotherdb'); 
//  When the data retrieval is complete, switch back to the default database connection.  
db_set_active('default'); 
?>

This is the basic operation of Drupal's database operation.

2. Drupal connection to multiple databases, but found a program error, this is what happened?

An error is reported when linking to multiple databases, mainly for the following reasons:

1. There was an error in SQL while connecting to another database. This is a human code error.
2. When connecting to a database, the table cannot be found in other databases. Even if SQL is correct, it should report an error.

Solution:
For the first case, please modify the SQL statement according to the SQL error, and it is resolved.
In the second case, check to see if the database connection is intersected. This means that the database connection has been moved to another place, but you intended to call a table from another database. Regarding database connection crossover, check whether the SQL statement following the db_set_active function is in the active database.

3. ES57en does not work properly when retrieving, adding, modifying, and deleting multiple databases.

SQL statements are not prefixed with tables in Drupal statements. By enclosing table statements with braces {}, you can prefix tables with tables in database operations.
For example: db_query('SELECT * FROM {table_in_anotherdb}');
However, a single database user with multiple database privileges can connect directly to the current database connection without setting the $db_url setting.
Set $db_prefix for cross-database operations:


$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_', 
);

When the above code is used, the current Drupal user information and other information all use usertable, so that multiple Drupal can share the same user information database usertable, where z_ represents the data table prefix.

Note:

users). The users table is used for the basic information of Drupal users. It can store UID and its basic fields shared by all users.
b).sessions table is used to store Drupal user Sessions, which can count the number of online users of all sites;
role). The role table is used to store the roles of all Drupal stations;
users_roles).users_roles holds permissions for all Drupal stations;
Using $db_prefix above, you can globally set which database to use for which table, and the prefix for which table to use. This is just for using the standard {table} in SQL statements of Drupal.

$db_prefix = $db_prefix = $db_prefix = $db_prefix = $db_prefix = $db_prefix;

Such as:


db_query("SELECT uid FROM test.z_table1 WHERE name = '%s' and pass = '%s'", $name, md5($pass));

The above SQL statement locates directly to the test database, the z_table table.
So when you encounter Drupal access, add, modify, delete multiple databases, data is not correctly written to the database or read empty data, please make sure that you control the database, data table location is correct.


4. Some function calls or controls other databases in Drupal

See the function framework code below:


function test_fuc() { 
  global $db_url; // Get global variables  
  $db_url['db_logs'] = 'mysqli://username:password@localhost/databasename'; 
  db_set_active('db_logs'); 
  $codehere; //  Place here operation db_logs database-connected SQL 
  db_set_active('default'); 
}

It's important to note that $db_url is a global variable and we need to use global in local functions:
global $db_url; // Get global variables 

After setting up the database, remember to use db_set_active('default'); Set the database connection back to default.


Related articles: