Jump to:

5542 Posts in 1739 Topics by 1225 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Connection to multiple databases

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2888 Views
  • linkoovi
    Avatar
    Community Member
    12 Posts

    Connection to multiple databases Link to this post

    Hi,

    I have to connect to a "external" db, (with different params:username/host/pass ), that holds some data, it has nothing to do with SS. I managed to connect and exe a query
    but I cant reconnect to the SS database back.

    How do i do this in SS 2.4?

  • patjnr
    Avatar
    Community Member
    101 Posts

    Re: Connection to multiple databases Link to this post

    Hi

    iam trying to do the same thing. how do you connect to a secondary db?

    thx

    PAt J

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Connection to multiple databases Link to this post

    how about the standard php way...

    //note use of true in mysql_connect, this ensure that the connection ($C) is a new
    //resource and not stomping all over the previous mysql_connect return value
    $C = mysql_connect( $MYSQL_SERVER, $MYSQL_USER, $MYSQL_PASS ,true);
    $cursor = mysql_query( $QUERY, $C );
    while ($r = mysql_fetch_array($cursor))
    {
       //do stuff here, e.g.
       $dataitem=$r['colname'];
    }

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Connection to multiple databases Link to this post

    I did this recently and this is how:
    In your _config.php you create two db connection settings, something along these lines:

    global $databaseConfig;

    $databaseConfig = array(
       'type' => 'MySQLDatabase',
       'server' => 'localhost',
       'username' => 'usernameA',
       'password' => 'passwordA',
       'database' => 'databaseA'
    );

    global $databaseConfig2;

    $databaseConfig2 = array(
       'type' => 'MySQLDatabase',
       'server' => 'localhost',
       'username' => 'usernameB',
       'password' => 'passwordB',
       'database' => 'databaseB'
    );

    Then in a function where you need both DBs, you do something like this:

    public function doStuff() {
       global $databaseConfig, $databaseConfig2;
       
       // connect to DB 2
       DB::connect($databaseConfig2);
       
       // create a query.. this will go to "databaseB"
       $query = new SQLQuery("*", "MyTable");

       $result = $query->execute();

       // do something with the result...
       
       // when done, switch back to the regular DB Config
       DB::connect($databaseConfig);
    }

  • patjnr
    Avatar
    Community Member
    101 Posts

    Re: Connection to multiple databases Link to this post

    The solution that worked for me is from Banal as my situation involved connecting to 3 databases.
    I created all my connection setting in _config.php. As said above, whenever i wanted to switch, I used

    DB::connect($databaseConfig_WP);


    and the most important part was to revert back to your default SS connection soon after.

    thx All

    PJ

    2888 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.