how to connect 2 databases simlutaneously using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neovantage
    New Member
    • Aug 2008
    • 245

    how to connect 2 databases simlutaneously using php

    Hey,
    I want to connect to 2 different MYSQL databases at the same time. Does somebody help me out that how can i do that closing one connection and selecting other and vice versa...?


    kind regards,
    Mohsin Rafique
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    If you are using the old MySQL extension, then all you would have to do is retrieve the return value of the mysql_connect function and use that value with the other mysql_ functions.

    [code=php]<?php
    $db1 = mysql_connect(' host1', 'usr', 'pwd');
    $db2 = mysql_connect(' host2', 'usr', 'pwd');

    mysql_select_db ('dbName', $db1);
    mysql_select_db ('dbName', $db2);

    mysql_query($sq l, $db1);
    mysql_query($sq l, $db2);
    ?>[/code]

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      Then one might ask: what if you want to join two tables from two databases together?

      easy, MySQL query allows you to select which database a table is from by appending the database number, a dot, and then the table name like so: db1.my_table JOIN db2.second_tabl e.


      Food for thought,



      Dan

      Comment

      Working...