0

Hi so I am trying to add in a connection to another database in Magento so I can query it, I have done the following:

Made a module and this is my config.xml:

<?xml version="1.0"?>
<config>
    <global>
        <resources>
            <externaldb_write>
                <connection>
                    <use>externaldb_database</use>
                </connection>
            </externaldb_write>
            <externaldb_read>
                <connection>
                    <use>externaldb_database</use>
                </connection>
            </externaldb_read>
            <externaldb_setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </externaldb_setup>
            <externaldb_database>
                <connection>
                    <host><![CDATA[IPADDRESS]]></host>
                    <username><![CDATA[USERNAME]]></username>
                    <password><![CDATA[PASSWORD]]></password>
                    <dbname><![CDATA[DATABASE]]></dbname>
                    <model>mysql4</model>
                    <type>pdo_mysql</type>
                    <active>1</active>
                </connection>
            </externaldb_database>
        </resources>
    </global>
</config>

And this is my code frontend:

<?php
  $resource = Mage::getSingleton('core/resource');
  $conn         = $resource->getConnection('externaldb_read');
  $results  = $conn->query('SELECT * FROM tablename');
  print_r($results);
?>

And I get the following error:
a:5:{i:0;s:125:"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'admin_magento_t.tablename' doesn't exist, query was: SELECT * FROM tablename";i:1;s:3026:"#0 /SERVERPATH/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)

I am confused why it says table "admin_magento_t.tablename" doesnt exist when I am only looking for "tablename"? I have tried this connection manually in PHP and it connects to the database fine

Can someone advise me on this?

Thanks!

0

This is how i use external DB.

Config.

<resources>
       <customcms_setup>
            <setup>
                <module>Test_Magewordpress</module>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </customcms_setup>
        <externaldb_write>
            <connection>
                <use>externaldb_database</use>
            </connection>
        </externaldb_write>
        <externaldb_read>
            <connection>
                <use>externaldb_database</use>
            </connection>
        </externaldb_read>
     <externaldb_database>
            <connection>
                <host><![CDATA[192.168.0.200]]></host>
                <username><![CDATA[test]]></username>
                <password><![CDATA[test]]></password>
                <dbname><![CDATA[test_wp]]></dbname>
                <model>mysql4</model>
                <type>pdo_mysql</type>
                <active>1</active>
            </connection>
        </externaldb_database>
    </resources>

connection made through below code successfully

$connection = Mage::getSingleton('core/resource')->getConnection('externaldb_read');
    $connection->query("SET NAMES 'utf8'");

    $sql = "SELECT * FROM wp6r_posts WHERE post_type = 'page' AND post_status = 'publish' ORDER BY post_date DESC";
    $rows = $connection->fetchAll($sql);

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.