如果你遇到这些问题:
1.Drupal如何连接到多个数据库?
2.Drupal连接到多个数据库后,但是发现程序报错,这是怎么了?
3.Drupal获取、添加、修改、删除多个数据库时,数据没有正确的写入数据库或者读取到空的数据,怎么解决?
4.只想在Drupal某个函数调用或控制其他数据库,但是失败了?
请认真看看后面的介绍,并如何解决你的问题。
一、Drupal如何连接到多个数据库?
允许Drupal连接多个数据库,需要转换$db_url为数组。
默认连接单个数据库的URL格式(字符串):
$db_url = 'mysqli://username:password@localhost/databasename';
$db_url = 'pgsql://username:password@localhost/databasename';
支持多个数据库的URL格式(数组):
$db_url['mydb'] = 'mysql://user:pwd@localhost/anotherdb';
$db_url['db3'] = 'mysql://user:pwd@localhost/yetanotherdb';
当查询一个不同的数据库时,简单地将数据库通过$db_url的引用键设置为当前活动的数据库,即可使用。
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引用:
设置完数据库后,记得使用db_set_active('default');,设置数据库连接恢复到默认。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

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.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.