How to connect to another database in magento
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><![CDATA[localhost]]></host> <username><![CDATA[db_username]]></username> <password><![CDATA[db_password]]></password> <dbname><![CDATA[db_name]]></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"); ?> |
Advertisements