0

I'm working on a magento project that's created by another firm. In a template file (app/design/frontend/companytheme/default/template/catalog/product/featured.phtml) they have:

$pdo = new PDO('mysql:host=localhost;dbname=dbname;'.$storeId,
    'dbusername',
    'dbpassword',
    array(
        PDO::MYSQL_ATTR_INIT_COMMAND    =>  "SET NAMES utf8",
        PDO::ATTR_ERRMODE               =>  PDO::ERRMODE_WARNING,
        PDO::ATTR_CASE                  =>  PDO::CASE_LOWER
        )
    );

But when I push this to my staging environment the database settings aren't equal. How can I create a config xml in app/etc and use these database settings in the specific template?

1

Create the database connection in /app/etc/local.xml :

Default_setup :

<config>
    <global>
        <install>
            <date><![CDATA[Tue, 13 Oct 2015 13:00:15 +0000]]></date>
        </install>
        <crypt>
            <key><![CDATA[a70ec048c7a4263e75f4855e010862e0]]></key>
        </crypt>
        <disable_local_modules>false</disable_local_modules>
        <resources>
            <db>
                <table_prefix><![CDATA[]]></table_prefix>
            </db>
            <default_setup>
                <connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[user]]></username>
                    <password><![CDATA[password]]></password>
                    <dbname><![CDATA[dbname]]></dbname>
                    <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
                    <model><![CDATA[mysql4]]></model>
                    <type><![CDATA[pdo_mysql]]></type>
                    <pdoType><![CDATA[]]></pdoType>
                    <active>1</active>
                </connection>
            </default_setup>
        </resources>
        <session_save><![CDATA[files]]></session_save>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <frontName><![CDATA[admin]]></frontName>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

Add another node here to handle the new database connection

<config>
    <global>
        <install>
            <date><![CDATA[Tue, 13 Oct 2015 13:00:15 +0000]]></date>
        </install>
        <crypt>
            <key><![CDATA[a70ec048c7a4263e75f4855e010862e0]]></key>
        </crypt>
        <disable_local_modules>false</disable_local_modules>
        <resources>
            <db>
                <table_prefix><![CDATA[]]></table_prefix>
            </db>
            <default_setup>
                <connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[user]]></username>
                    <password><![CDATA[password]]></password>
                    <dbname><![CDATA[dbname]]></dbname>
                    <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
                    <model><![CDATA[mysql4]]></model>
                    <type><![CDATA[pdo_mysql]]></type>
                    <pdoType><![CDATA[]]></pdoType>
                    <active>1</active>
                </connection>
            </default_setup>

        <second_setup>
                <connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[user]]></username>
                    <password><![CDATA[password]]></password>
                    <dbname><![CDATA[dbname]]></dbname>
                    <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
                    <model><![CDATA[mysql4]]></model>
                    <type><![CDATA[pdo_mysql]]></type>
                    <pdoType><![CDATA[]]></pdoType>
                    <active>1</active>
                </connection>
            </second_setup>

        </resources>
        <session_save><![CDATA[files]]></session_save>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <frontName><![CDATA[admin]]></frontName>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

Connection new database like:

$config = Mage::getConfig()->getResourceConnectionConfig('second_setup');
        $dbConfig = array(
        'host'      => $config->host,
        'username'  => $config->username,
        'password'  => $config->password,
        'dbname'    => $config->dbname
    );

$connection = Zend_Db::factory('Firebird', $dbConfig);

Use Queries like:

$connection->fetchAll("SELECT * FROM table");
$connection->update('table',array('field'=>'value'),$where);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.