Drupal7连接多个数据库及常见问题解决_PHP
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');,设置数据库连接恢复到默认。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
