search
top

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 Responses to “Connect different database from Magento”

  1. vicky says:

    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’);
    ?>

  2. Deepak says:

    It is not work for me.

    • BRP says:

      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.

  3. subbu says:

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

  4. Virus says:

    Superb post, works very well for me.. :)

  5. Tim says:

    Very nice post. Thank you a lot.

  6. Ali says:

    Can we configure magento to use separate database connection for session management.

    • Pragnesh says:

      @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

  7. We have developed a new Magento Extenstion now available on Magento connection for external DB connection.

    http://www.magentocommerce.com/magento-connect/the3tree/extension/6428/ttt_edb

  8. Floppy Max says:

    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

  9. Awara says:

    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

top