Friday, September 21, 2012

Magento : Create an external database connection


Add the following code  to your config.xml under global tag

Consider Bd_test module

<global>

        <resources>

            <test_write>

                <connection>

                    <use>test_database</use>

                </connection>

            </test_write>

            <test_read>

                <connection>

                    <use>test_database</use>

                </connection>

            </test_read>

            <test_setup>

                <connection>

                    <use>core_setup</use>

                </connection>

            </test_setup>

            <test_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>

            </test_database>

        </resources>

    </global>


Get data

    $resource   = Mage::getSingleton('core/resource');

    $conn       = $resource->getConnection('test_read');

    $results    = $conn->query('SELECT * FROM TableName');

    print_r($results)

2 comments:

  1. Very useful piece of information i have got here.This is so good to have visited this post.Would visit for more such useful information.

    ReplyDelete
  2. Magento is best for Ecommerce websites and easy to learn.
    Magento is robust, scalable, rich in features and user friendly. It has ecommerce features that in that past would have cost prohibitive.
    Magento Development India

    ReplyDelete