Home Backend Development PHP Tutorial Drupal7连接多个数据库及常见问题解决_PHP

Drupal7连接多个数据库及常见问题解决_PHP

Jun 01, 2016 am 11:56 AM

Drupal

如果你遇到这些问题:
1.Drupal如何连接到多个数据库?
2.Drupal连接到多个数据库后,但是发现程序报错,这是怎么了?
3.Drupal获取、添加、修改、删除多个数据库时,数据没有正确的写入数据库或者读取到空的数据,怎么解决?
4.只想在Drupal某个函数调用或控制其他数据库,但是失败了?
请认真看看后面的介绍,并如何解决你的问题。
一、Drupal如何连接到多个数据库?
允许Drupal连接多个数据库,需要转换$db_url为数组。
默认连接单个数据库的URL格式(字符串):
复制代码 代码如下:$db_url = 'mysql://username:password@localhost/databasename';
$db_url = 'mysqli://username:password@localhost/databasename';
$db_url = 'pgsql://username:password@localhost/databasename';

支持多个数据库的URL格式(数组):
复制代码 代码如下:$db_url['default'] = 'mysql://drupal:drupal@localhost/drupal';
$db_url['mydb'] = 'mysql://user:pwd@localhost/anotherdb';
$db_url['db3'] = 'mysql://user:pwd@localhost/yetanotherdb';

当查询一个不同的数据库时,简单地将数据库通过$db_url的引用键设置为当前活动的数据库,即可使用。
复制代码 代码如下:db_set_active('mydb');
db_query('SELECT * FROM table_in_anotherdb');
// 当数据获取完成后,切换回默认的数据库连接。
db_set_active('default');
?>

这是Drupal的数据库操作的基本操作。

二、Drupal连接到多个数据库后,但是发现程序报错,这是怎么了?

链接到多个数据库时出现报错,主要可能以下原因:

1.连接到其他数据库时,SQL出错了,这个是人为的代码错误;
2.连接数据库时交叉了,所以在其他数据库里找不到数据表,即使SQL正确,也要报错;

解决方法:
针对第一种情况,请根据SQL报错,来修改SQL语句,就解决了。
第二种情况,请检查数据库连接是否交叉了,意思就是本来想调用另外数据库的数据表,但是数据库连接已经换到其他地方了。关于数据库连接交叉,请仔细检查db_set_active这个函数之后的SQL语句,是否在active数据库里。

三、Drupal获取、添加、修改、删除多个数据库时,没有正常工作?

1、在Drupal中SQL语句可以不带数据表的前缀,只需要用大括号{}包含table就可以在数据库操作时加上数据表的前缀。
例如:db_query('SELECT * FROM {table_in_anotherdb}');
但是一个数据库用户,如果拥有多个数据库的权限时,可以不用在$db_url设置连接到数据库,直接在当前数据库连接上操作就行了。
设置$db_prefix来实现跨数据库操作:
复制代码 代码如下:
$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_',
);

上面的代码作用时,当前Drupal的用户等信息全部使用usertable,这样多个Drupal就可以共用一个用户信息数据库usertable,其中z_代表数据表的前缀。

注意:

a).users表用于存在Drupal用户的基本信息,可以存储所有用户共用的UID及其基本字段;
b).sessions表用于存放Drupal用户Sessions,可以统计所有站点的在线用户量;
c).role表用于存放所有Drupal站的角色;
d).users_roles存放所有Drupal站的权限;
通过上面的$db_prefix可以全局设置使用那个表要用到那个数据库,以及那个表的前缀,这个只是方便在Drupal的SQL语句中使用标准的{table}。

2、如果不通过$db_prefix来设置,那么最直白的方法就是直接把数据库 表名在SQL语句中。

例如:
复制代码 代码如下:
db_query("SELECT uid FROM test.z_table1 WHERE name = '%s' and pass = '%s'", $name, md5($pass));

上面的SQL语句直接定位到test数据库,z_table数据表。
所以当你遇到Drupal获取、添加、修改、删除多个数据库时,数据没有正确的写入数据库或者读取到空的数据,请明确你所控制的数据库、数据表位置是否正确。


四、在Drupal某个函数调用或控制其他数据库

请看下面的function框架代码:
复制代码 代码如下:
function test_fuc() {
  global $db_url; //获取全局变量
  $db_url['db_logs'] = 'mysqli://username:password@localhost/databasename';
  db_set_active('db_logs');
  $codehere; // 此处放置操作db_logs数据库连接的SQL
  db_set_active('default');
}

特别要主要,$db_url是全局变量,需要在局部函数中用global引用:
复制代码 代码如下:global $db_url; //获取全局变量
设置完数据库后,记得使用db_set_active('default');,设置数据库连接恢复到默认。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

See all articles