DRUPAL7 connecting multiple databases and solving common problems _php tutorials

Source: Internet
Author: User
Tags sql error drupal
If you encounter these problems:
1.Drupal How do I connect to multiple databases?
2.Drupal connected to multiple databases, but found that the program error, what is the matter?
3.Drupal get, add, modify, delete multiple databases, 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 fail?
Please take a serious 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 and convert $db_url to arrays.
Default connection URL format (string) for a single database:
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 ';

URL formats (arrays) that support multiple databases:
Copy the 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, it is simple to set the database through the $db_url reference key to the currently active database.
Copy the Code code as follows: 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.

Second, Drupal connected to multiple databases, but found that the program error, what is the matter?

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

1. When connecting to other databases, SQL error, this is a human code error;
2. The database is connected to cross, so in other databases can not find the data table, even if the SQL is correct, but also to error;

Workaround:
For the first case, according to the SQL error, to modify the SQL statement, it is resolved.
In the second case, check that the database connection is crossed, meaning that you want to call the data table of the other database, but the database connection has been swapped elsewhere. For database connection crossings, double-check the SQL statements that follow the Db_set_active function in the active database.

Third, Drupal get, add, modify, delete multiple databases, not working properly?

1. In Drupal, the SQL statement can be prefixed with the data table, with the curly braces {} containing the table to prefix the database operation with the data table.
For example: Db_query (' SELECT * from {TABLE_IN_ANOTHERDB} ');
However, a database user, if you have multiple database permissions, you can not connect to the database in the $db_url settings, directly on the current database connection operation on the line.
Set $db_prefix to implement cross-database operations:
Copy the 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_ ',
);

When the code above works, the current Drupal users and other information all use usertable, so that multiple Drupal can share a user information database usertable, where z_ represents the prefix of the data table.

Attention:

A) The users table is used for basic information about Drupal users and can store all user-shared UID and its basic fields;
b). Sessions tables are 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 can be used globally to set the database to use for that table, and the prefix of that table, which is just handy for using standard {table} in Drupal's SQL statements.

2, if not through $db_prefix to set, then the most straightforward way is to directly put the database table name in the SQL statement.

For example:
Copy the Code code as follows:
Db_query ("Select uid from test.z_table1 WHERE name = '%s ' and pass = '%s '", $name, MD5 ($pass));

The above SQL statement navigates directly to the test database, z_table the data table.
So when you encounter Drupal fetching, adding, modifying, deleting multiple databases, the data is not written correctly to the database or read to empty data, please make sure you control the database, data table location is correct.


Iv. in Drupal a function calls or controls other databases

Take a look at the following function framework code:
Copy the 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 to place Operations Db_logs database connections here
Db_set_active (' Default ');
}

In particular, $db _url is a global variable that needs to be referenced in a local function with global:
Copy the Code Code as follows: Global $db _url; Get Global variables
After setting up the database, remember to use Db_set_active (' Default '), and set the database connection back to default.

http://www.bkjia.com/PHPjc/736821.html www.bkjia.com true http://www.bkjia.com/PHPjc/736821.html techarticle If you encounter these issues: how does 1.Drupal connect to multiple databases? 2.Drupal connected to multiple databases, but found that the program error, what is the matter? 3.Drupal get, add 、...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.