How to connect to another database in magento :
For Connecting to another database rather than using the current database you need to define new resource in config.php and tell magento to use this resource.
Config.php using External Database
<?xml version="1.0"??> <config> <modules> <tutorialsplane_anotherdb> <version>0.1.1</version> </tutorialsplane_anotherdb> </modules> <global> <resources> <anotherexternaldb_write> <connection> <use>anotherexternal_database</use> </connection> </anotherexternaldb_write> <anotherexternaldb_read> <connection> <use>anotherexternal_database</use> </connection> </anotherexternaldb_read> <anotherexternaldb_setup> <connection> <use>core_setup</use> </connection> </anotherexternaldb_setup> <anotherexternaldb_database> <connection> <host></host> <username></username> <password></password> <dbname></dbname> <model>mysql4</model> <type>pdo_mysql</type> <active>1</active> </connection> </anotherexternaldb_database> </resources> </global> </config> |
Now you can access Database as below :
Access Database
<?php $resource = Mage::getSingleton('core/resource'); $connection = $resource-?>getConnection('anotherexternaldb_read'); $data = $connection->query("SELECT * FROM customers"); ?> |