简体   繁体   English

使用JDatabase连接到Joomla中的多个数据库

[英]Connecting to Multiple Databases in Joomla Using JDatabase

It is possible to create connections to multiple databases using the JDatabase::getInstance() method. 可以使用JDatabase::getInstance()方法创建与多个数据库的JDatabase::getInstance() The following link points to a tutorial that describes how to create a helper file that makes it easy to create multiple JDatabase instances that point to different databases. 以下链接指向一个教程,该教程描述了如何创建一个帮助文件,该文件使创建指向不同数据库的多个JDatabase实例变得容易。 You can create the database instances in the following manner using the custom helper class. 您可以使用定制帮助程序类以以下方式创建数据库实例。

$db = JFactory::getDBO();
$db2 = MyDataHelper::getDBO2();
$db3 = MyDataHelper::getDBO3();

You can still get the default database object normally using JFactory. 您仍然可以正常使用JFactory获取默认数据库对象。

You can add your own array of options to JDatabaseDriver , like so: 您可以将自己的选项数组添加到JDatabaseDriver ,如下所示:

First database: 第一个数据库:

$db = JFactory::getDbo();

Second database: 第二个数据库:

$option2 = array();
$option2['driver']   = 'mysqli';     // Database driver name
$option2['host']     = 'localhost';  // Database host name
$option2['user']     = 'DB_USER';    // User for database authentication
$option2['password'] = 'DB_PASS';    // Password for database authentication
$option2['database'] = 'DB_NAME';    // Database name
$option2['prefix']   = 'jos_';       // Database prefix (may be empty)

$db2 = JDatabaseDriver::getInstance($option2);

Third database: 第三个数据库:

$option3 = array();
$option3['driver']   = 'mysqli';     // Database driver name
$option3['host']     = 'localhost';  // Database host name
$option3['user']     = 'DB_USER';    // User for database authentication
$option3['password'] = 'DB_PASS';    // Password for database authentication
$option3['database'] = 'DB_NAME';    // Database name
$option3['prefix']   = 'jos_';       // Database prefix (may be empty)

$db3 = JDatabaseDriver::getInstance($option3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM