-
- How to Add a User to a Joomla Group Programmatically
- How to Send Emails in Joomla with PHP
- How to Connect to an External Database in Joomla with PHP
- How to Check if the Current Page Is the Homepage in Joomla with PHP
- How to Check If the Current User is Super User in Joomla with PHP
- How to Create a User Account in Joomla with PHP
- How to Create an Article in Joomla with PHP
- PHP Scripts Collection
How to Connect to an External Database in Joomla with PHP
Heads up! This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.
Below is a PHP snippet allowing you to connect to an external database using the Joomla API.
// Define the external database driver options
$options = [
'driver' => 'mysql', // The Database driver name
'host' => 'db.myhost.com', // Database host name
'user' => 'username', // User for database authentication
'password' => 'password', // Password for database authentication
'database' => 'database_name', // Database name
'prefix' => 'abc_' // Database prefix (may be empty)
];
if (version_compare(JVERSION, '4.0', '>=')) {
// Joomla 4+
$db = (new \Joomla\Database\DatabaseFactory())>getDriver('mysqli', $options);
} else {
// Joomla 3
$db = \Joomla\Database\DatabaseDriver::getInstance($options);
}
// Use the database driver for your queries
$query = $db→getQuery(true);
$query
→select($db→quoteName(array('column1', 'column2')))
→from($db→quoteName('#__table'));
$db→setQuery($query);
$results = $db→loadObjectList();
Last updated on May 22nd 2025 10:05