Home > Joomla, PHP > Connect to an external database from a Joomla extension

Connect to an external database from a Joomla extension

October 28th, 2011 Leave a comment Go to comments

Hello guys, sometimes we may need to connect to an external database from a custom made joomla extension. It is possible to connect to an external database with the help of JDatabase class. The following sample function connects to an external database and execute a query in the database and returns the result

function getDetails()
{
     $options = array();
     $options['driver']   = 'mysql';
     $options['host']     = 'localhost';
     $options['user']     = 'dbuser';
     $options['password'] = 'dbpassword';
     $options['database'] = 'databasename';
     $options['prefix']   = 'tblprefix_';
     $db = & JDatabase::getInstance($options);
     $query = "SELECT * FROM #__example_table";
     $db->setQuery($query);
     return $db->loadAssocList();
}
Categories: Joomla, PHP Tags: