Connect two different database server using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tokcy
    New Member
    • Sep 2008
    • 45

    Connect two different database server using PHP

    Hi everyone,

    I want to connect two different database server using php/mysql.
    Suppose i have some dat on www.xyz.com and i want to select that data on www.abc.com
    How do i integrate two different database server please some body tell me...

    Thanks in advance
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    do you know how to connect to one database? if not look online for PHP tutorial and read the manual at php.org

    I don't even know what database you're using, if MySQL then php has native functions for that.

    If you know how to connect to one, then you can connect to multiple.




    Dan

    Comment

    • tokcy
      New Member
      • Sep 2008
      • 45

      #3
      of course i know how to connect one database but i am facing the problem like if i connect two different databse on local server it works fike and when i upload that file to remote server its not working and i am using PHP 5.0/MYSQL .

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        to work with a remote server you usually need permissions to access it (many ISP servers allow only localhost access). otherwise you need to make sure to always pass the correct resource link. (if you're using PDO you might get away with 2 DB objects, though)

        Comment

        • tokcy
          New Member
          • Sep 2008
          • 45

          #5
          i am connecting two database server with this code.

          Code:
          For first server
          <?php
          $hostname = "hostname";
          $username = "username"; 
          $password = "password";  
          $con = mysql_connect($hostname,$username,$password) or die("Database not connected");
          mysql_select_db("database");  
          ?>
          
          For second server
          <?php
          $hostname = "hostname1";
          $username = "username1"; 
          $password = "password1";  
          $con = mysql_connect($hostname,$username,$password) or die("Database not connected");
          mysql_select_db("database1");  
          ?>

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by tokcy
            i am connecting two database server with this code.
            this code will probably get you in trouble if you want to use both connections in the same script.

            - the second connection will overwrite the first one (you need to use different variable names)
            - if you're using more than one connection, you must (in a logical sense) pass the connection resource to every mysql_* function.

            my personal recommendation: use PDO. it boils down to using 2 different DB objects, which is IMO easier to track and maintain (beside the various other advantages that come with PDO (Exceptions, Prepared Statements, advanced data fetching, …)

            Comment

            • tokcy
              New Member
              • Sep 2008
              • 45

              #7
              Sorry for prev reply actually i had not notice that i am using same variable name in prev reply

              my actual code is:

              Code:
              <?php
              $r_hostname = "hostname1";
              $r_username = "username1";
              $r_password = "pass1";
              $link = mysql_connect($r_hostname,$r_username,$r_password);
              $db = mysql_select_db("db_name1",$link);
              ?>
              <?php
              $hostname = "hostname2";
              $username = "username2"; 
              $password = "pass2";  
              $con = mysql_connect($hostname,$username,$password);
              $db1 = mysql_select_db("db_name2",$con);  
              if ($con)
               {
              	if ($db1) 
              	{
              		echo "connected and selected ok";
              	}
              	else
                	{
              		echo "connected and selected failed";
              	}
              } 
              else
              {
              echo "failed to connect to server";
              }
              ?>

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                does it work now?

                you might think over, how to verify the server connections, currently you completely neglect server1 errors.

                line 13 would fail if the server connection (line 12) failed.

                Comment

                • tokcy
                  New Member
                  • Sep 2008
                  • 45

                  #9
                  No its not working...

                  And server1 is that server where my registration page resides and i want to insert all data into database which is on server2 .
                  But i am not able to do this...

                  Thanks for your kind reply

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    any error messages? can you confirm both connections? have you access permission on server2?

                    Comment

                    • tokcy
                      New Member
                      • Sep 2008
                      • 45

                      #11
                      I am getting this error

                      Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'mysql50-30.wcl' (11001).

                      and for permission i have to check but how will i check that server2 has given me permission to access database. If not then how will i enable the access...

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        that's a strange host name, usually the host name is identical to the server name.

                        as for the permissions, ask the Administrator of server2.

                        Comment

                        • tokcy
                          New Member
                          • Sep 2008
                          • 45

                          #13
                          If i am using only on single server this name as hostname ''mysql50-30.wcl'' then its working fine i can do all the think like select, delete and insert command. on this server but its not working when i am trying to connect on another server.

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            what do you get for "mysql.max_link s" in php.ini (see phpinfo() in the MySQL section)?

                            Comment

                            • tokcy
                              New Member
                              • Sep 2008
                              • 45

                              #15
                              its showing unlimited

                              Comment

                              Working...