6
votes

How do you get the database name from Magento?

I have seen functions like the one below that can get the table name.

$orderItemTable = Mage::getSingleton('core/resource')->getTableName('sales/order_item');

I was hoping that there was some sort of function like this:

Mage::getSingleton('core/resource')->getDatabaseName();

Thanks in advance for any ideas.

3

3 Answers

17
votes

Each module can specify it's own connection so you are right to go via a model.

$config = Mage::getResourceModel('sales/order')->getConnection()->getConfig();
// $config is an array
$dbname = $config['dbname'];

If you're only interested in the default a slightly more efficient way might be:

$dbname = (string)Mage::getConfig()->getNode('global/resources/default_setup/connection/dbname');
3
votes

To get the db name try

Mage::getConfig()->getResourceConnectionConfig('default_setup')->dbname;

See How to get magento database details

1
votes

It is always possible to get the current connection from Mage::getSingleton('core/resource')->getConnection('core_write'), quite specially if you use only one database.

$configArray = Mage::getSingleton('core/resource')->getConnection('core_write')->getConfig();
$db = $configArray['name'];

But it's comming with a zend adapter Zend_Config

// Create the object-oriented wrapper upon the configuration data
$config = new Zend_Config($configArray);
$db = $config->get('dbname');