Connect two database at a time

7 replies
I have one question. Can i connect one hosting database to anther hosting.

Example:
Website 1: www.abc.com is hosted in Hosting 1 and Website 2: www.xyz.com is hosted in Hosting 2
When someone open www.abc.com i want to add data/value in hosting 2 database. Website 1 also have database i don't want to close his connection.
#connect #database #time
  • Profile picture of the author jamesthaker
    Hello,
    Create two instances for the databases.
    <?php
    variable a=mysql_connect('user_name','password');
    variable ins1=mysql_select_db('DB1',variable a);
    variable ins2 =mysql_select_db('DB2',variable a);
    ?>
    Then you can use the instances where ever you need.
    {{ DiscussionBoard.errors[8815649].message }}
    • Profile picture of the author David Beroff
      Sure; there's no reason why you can't have two connections. Obviously, you'll need to specify the remote host name, rather than using the local host, but that should be fine.
      Signature
      Put MY voice on YOUR video: AwesomeAmericanAudio.com
      {{ DiscussionBoard.errors[8815919].message }}
      • Profile picture of the author lovefax89
        Originally Posted by David Beroff View Post

        Sure; there's no reason why you can't have two connections. Obviously, you'll need to specify the remote host name, rather than using the local host, but that should be fine.
        I don't understand fully can you please explain more like give me example or some thing more ?

        What is remote host ? and can i open two database connection at a time ?
        {{ DiscussionBoard.errors[8816146].message }}
        • Profile picture of the author David Beroff
          Originally Posted by lovefax89 View Post

          I don't understand fully can you please explain more like give me example or some thing more ?

          What is remote host ? and can i open two database connection at a time ?
          Yes, you can. In your example above, abc would be the local host, and xyz is the remote host. When the abc website wants to access the database on xyz, the connection would appear like:

          PHP Code:
          try {
             $ 
          db = new PDO"mysql:host=mysql.xyz.com;dbname=xyzdb"
                            
          "username""password",
                            array( 
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                                   
          PDO::ATTR_EMULATE_PREPARES => false
                                   
          PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ) );
          } catch (
          PDOException e) {
             echo $ 
          e->getMessage();

          (Forgive the spaces after the dollar signs; WF won't let me post without them.)
          Signature
          Put MY voice on YOUR video: AwesomeAmericanAudio.com
          {{ DiscussionBoard.errors[8816247].message }}
    • Profile picture of the author David Beroff
      Originally Posted by jamesthaker View Post

      Create two instances for the databases.
      Please re-read the original question.
      Signature
      Put MY voice on YOUR video: AwesomeAmericanAudio.com
      {{ DiscussionBoard.errors[8815923].message }}
  • Profile picture of the author KirkMcD
    You might need to ask your host to allow you to do this too.
    {{ DiscussionBoard.errors[8816520].message }}
  • Profile picture of the author Lanii
    Originally Posted by lovefax89 View Post

    I have one question. Can i connect one hosting database to anther hosting.

    Example:
    Website 1: www.abc.com is hosted in Hosting 1 and Website 2: www.xyz.com is hosted in Hosting 2
    When someone open www.abc.com i want to add data/value in hosting 2 database. Website 1 also have database i don't want to close his connection.
    Hey,

    If your both hosting is using cPanel, there is place where u can allow remove connection.


    On www.xyz.com, you add there IP of www.abc.com and backwards on www.abc.com cPanel.

    After that, u can access xyz database from abc using IP of xyz, instead of "localhost" when u connect to database.
    {{ DiscussionBoard.errors[8819035].message }}

Trending Topics