简体   繁体   中英

Connecting to Multiple Databases in Joomla Using JDatabase

It is possible to create connections to multiple databases using the JDatabase::getInstance() method. 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. 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.

You can add your own array of options to JDatabaseDriver , like so:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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