Bala
Bala

Reputation: 1

How can I use the default Joomla's database username and password, to connect to another database?

Basically I just want to connect to the same host but a different database in a Joomla module. So how can i get the user/pass info from the config to avoid the need to hard code or parametrize that info, since its already there.

Thanks

Upvotes: 0

Views: 766

Answers (2)

Kristian Hildebrandt
Kristian Hildebrandt

Reputation: 1528

Get config values like this:

$app = JFactory::getApplication();    
echo $app->getCfg('user'); 
echo $app->getCfg('password'); 

Upvotes: 0

alghimo
alghimo

Reputation: 2899

You can use this code ( it's the same code that's in the JFactory::getDBO() method ).

jimport('joomla.database.database');
jimport('joomla.database.table');

$conf = JFactory::getConfig();

$host   = $conf->get('host');
$user   = $conf->get('user');
$password   = $conf->get('password');
$database   = 'YOUR_DATABASE_NAME';
$prefix = $conf->get('dbprefix'); //***Change this if the dbprefix is not the same!***
$driver = $conf->get('dbtype');

$options    = array ('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix);

$db = JDatabase::getInstance($options);

I hope it helped!

Upvotes: 2

Related Questions