Connect different database from Magento

Some of you might have worked on Magento.

I am listing here points for connecting another database from Magento.

1) Suppose your database for Magento Project is named ‘magento’.

2) There is one other database named ‘wp’.

3) For using this database inside Magento, you need to setup connection in ‘config.xml’ file which resides in ‘urmagentodirectory/app/etc’

4) In ‘config.xml’ there is a tag ‘’ inside which default connection is setup, you need to write this code under this.

<wp_setup>
	<connection>
	<host>< ![CDATA[hostname]]></host>
	<username>< ![CDATA[username]]></username>
	<password>< ![CDATA[password]]></password>
	<dbname>< ![CDATA[wp]]></dbname>
	<model>mysql4</model>
	<initstatements>SET NAMES utf8</initstatements>
	<type>pdo_mysql</type>
	<active>1</active>
	</connection>
</wp_setup>
<wp_write>
	<connection>
	<use>wp_setup</use>
	</connection>
</wp_write>
<wp_read>
	<connection>
	<use>wp_setup</use>
	</connection>
</wp_read>

5) Now you can use this connection by following

<?php
$read = Mage::getSingleton('core/resource')->getConnection('wp_read');
$write = Mage::getSingleton('core/resource')->getConnection('wp_write');
?>

16 thoughts on “Connect different database from Magento

  1. sir where place this code. i am placing this code in my another database admin but its show error class mage no found
    getConnection(‘wp_read’);
    $write = Mage::getSingleton(‘core/resource’)->getConnection(‘wp_write’);
    ?>

    1. Please, could you tell me where to put the code (getConnection(‘wp_read’);
      $write = Mage::getSingleton(‘core/resource’)->getConnection(‘wp_write’);
      ?>) exactly becouse I cant get it work.
      Thanks.

  2. plz tell where to place in path
    getConnection(‘wp_read’);
    $write = Mage::getSingleton(‘core/resource’)->getConnection(‘wp_write’);
    ?>
    in magento

    1. @Ali – yes you can use it, but than you will need to modify the session model, and put your custom code at many places, which will quite troublesome

  3. Hello everyone,

    I’m working on Magento Enterprise v1.9.0.0.
    I need to connect to the database and run SQL directly to query tables.

    Here is what I’ve used to do that.

    $query = “…SQL QUERY…”;
    $databaseConnection = Mage::getSingleton(‘core/resource’)->getConnection(‘core_read’);
    $result = $databaseConnection->fetchAll($query);

    I have Magento Community v1.4.1.1 and Magento Enterprise v1.9.0.0 installed on my local computer.
    So in order to test, I’ve run the above code on my local machine, and all works fine.

    But it doesn’t work on live server.
    Magento Enterprise v1.9.0.0 is installed on the live server and online store is running.
    All works fine on live store and nothing special happens.

    But I’ve uploaded this code to the server, it doesn’t work.
    And further, I can’t get any PHP/MySQL error message.

    Here is more detailed codes which I used to track run time.

    // Debug Output
    $this->OutputDebugLogo(“”);
    $this->OutputDebugLogo(“Query : #”, $query);

    $databaseConnection = Mage::getSingleton(‘core/resource’)->getConnection(‘core_read’);
    $result = $databaseConnection->fetchAll($query);

    // Debug Output
    $this->OutputDebugLogo(“Result : #”, $result);
    $this->OutputDebugLogo(“”);

    I can be sure there is no error on SQL statement.
    And the function OutputDebugLogo() is made by myself to output debug message with variables.

    Here is what I’ve got after running this script on my local machine.

    Query :
    ‘ SELECT … FROM … WHERE … ‘

    Result:
    array ( 0 => array ( ‘items’ => ‘a:2:{s:3:”MEN”;a:11:{s:9:… )

    And here you can see what I’ve got on the live server.

    Query :
    ‘ SELECT … FROM … WHERE … ‘

    As you can see, there is no PHP/MySQL error message.
    And we can say the script wass stopped running at database code block.

    I can’t make any progress to find the main cause.
    Why did it happen?
    Can anyone know it?

    I’m really looking forward to getting help from everyone.

    Best Regards,
    Floppy

  4. Very nice post, I solved my database query problem. Please keep it up. I wana see more posts regarding Magento, Share your knowledge 😛

Leave a Reply to Decrypt Web Cancel reply

Your email address will not be published. Required fields are marked *

Scroll to top