Using conection from two different data bases in same php file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shalini Bhalla
    New Member
    • Dec 2007
    • 190

    #1

    Using conection from two different data bases in same php file

    can i make use of two different connection using different database in a same php file , if yes , then how
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Just make sure you keep seperare copies of the Resource link Identifier for each connection
    Code:
    $db1 = mysql_connect........
    $db2 = mysql_connect........
    And always use the identifiers for every query etc

    Comment

    • sandeepsandeep
      New Member
      • Dec 2006
      • 50

      #3
      Originally posted by Shalini Bhalla
      can i make use of two different connection using different database in a same php file , if yes , then how

      Hi Shalini,

      you can make different connection using different database.

      first make the two object of the database .
      For example two DB
      $DB_HOST1=array (
      "Host"=>"localh ost",
      "Username"=>"ro ot",
      "Password"= >"",
      "Database"=>"DB 1",
      );
      $DB_HOST2=array (
      "Host"=>"ho st name or IP",
      "Username"=>"ab c",
      "Password"=>"ab c",
      "Database"=>"DB 2",
      );

      suppose first is MYSQL

      $conn1=mysql_pc onnect($DB_HOST 1[Host"], $DB_HOST1[["Username"], $DB_HOST1["Password"]);mysql_select_db ($DB_HOST1[["Database"],$conn1);

      and second is oracle then

      strConn="(DESCR IPTION=(ADDRESS =(PROTOCOL=TCP)
      (HOST = $DB_HOST2["Host"])
      (PORT=1521)
      )
      (CONNECT_DATA=
      (SERVER=DEDICAT ED)
      (SERVICE_NAME=$ DB_HOST2["Database"])
      )
      )

      then connect
      $conn2=OCIPLogo n(DB_HOST2["Username"], DB_HOST2["Password"], $strConn);

      Now u have two connection so u can use two databess who is required.
      use the object of database in MYSQL_QUERY("sq l statement",conn ection id object); in case of mysql


      but better way to make a class and two different function of different database so when u required other DB them called the class and call a diifferent DB call

      Regards,
      Sandeep Agrawal

      Comment

      • Shalini Bhalla
        New Member
        • Dec 2007
        • 190

        #4
        Thanks a lot sir for ur help ....

        Comment

        Working...