Connecting Tech Pros Worldwide Help | Site Map

Connect two different database server using PHP

  #1  
Old July 1st, 2009, 03:22 PM
Member
 
Join Date: Sep 2008
Posts: 42
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
  #2  
Old July 1st, 2009, 11:32 PM
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,075

re: Connect two different database server using PHP


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
  #3  
Old July 2nd, 2009, 05:33 AM
Member
 
Join Date: Sep 2008
Posts: 42

re: Connect two different database server using PHP


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 .
  #4  
Old July 2nd, 2009, 06:42 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: Connect two different database server using PHP


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)
  #5  
Old July 2nd, 2009, 07:25 AM
Member
 
Join Date: Sep 2008
Posts: 42

re: Connect two different database server using PHP


i am connecting two database server with this code.

Expand|Select|Wrap|Line Numbers
  1. For first server
  2. <?php
  3. $hostname = "hostname";
  4. $username = "username"; 
  5. $password = "password";  
  6. $con = mysql_connect($hostname,$username,$password) or die("Database not connected");
  7. mysql_select_db("database");  
  8. ?>
  9.  
  10. For second server
  11. <?php
  12. $hostname = "hostname1";
  13. $username = "username1"; 
  14. $password = "password1";  
  15. $con = mysql_connect($hostname,$username,$password) or die("Database not connected");
  16. mysql_select_db("database1");  
  17. ?>
  18.  
  #6  
Old July 2nd, 2009, 08:18 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: Connect two different database server using PHP


Quote:
Originally Posted by tokcy View Post
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, …)
  #7  
Old July 2nd, 2009, 08:27 AM
Member
 
Join Date: Sep 2008
Posts: 42

re: Connect two different database server using PHP


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

my actual code is:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $r_hostname = "hostname1";
  3. $r_username = "username1";
  4. $r_password = "pass1";
  5. $link = mysql_connect($r_hostname,$r_username,$r_password);
  6. $db = mysql_select_db("db_name1",$link);
  7. ?>
  8. <?php
  9. $hostname = "hostname2";
  10. $username = "username2"; 
  11. $password = "pass2";  
  12. $con = mysql_connect($hostname,$username,$password);
  13. $db1 = mysql_select_db("db_name2",$con);  
  14. if ($con)
  15.  {
  16.     if ($db1) 
  17.     {
  18.         echo "connected and selected ok";
  19.     }
  20.     else
  21.       {
  22.         echo "connected and selected failed";
  23.     }
  24. else
  25. {
  26. echo "failed to connect to server";
  27. }
  28. ?>
  29.  
  #8  
Old July 2nd, 2009, 08:32 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: Connect two different database server using PHP


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.
  #9  
Old July 2nd, 2009, 09:05 AM
Member
 
Join Date: Sep 2008
Posts: 42

re: Connect two different database server using PHP


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
  #10  
Old July 2nd, 2009, 09:07 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: Connect two different database server using PHP


any error messages? can you confirm both connections? have you access permission on server2?
  #11  
Old July 2nd, 2009, 09:20 AM
Member
 
Join Date: Sep 2008
Posts: 42

re: Connect two different database server using PHP


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...
  #12  
Old July 2nd, 2009, 09:24 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: Connect two different database server using PHP


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.
  #13  
Old July 2nd, 2009, 10:44 AM
Member
 
Join Date: Sep 2008
Posts: 42

re: Connect two different database server using PHP


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.
  #14  
Old July 2nd, 2009, 10:50 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: Connect two different database server using PHP


what do you get for "mysql.max_links" in php.ini (see phpinfo() in the MySQL section)?
  #15  
Old July 2nd, 2009, 10:56 AM
Member
 
Join Date: Sep 2008
Posts: 42

re: Connect two different database server using PHP


its showing unlimited
  #16  
Old July 2nd, 2009, 10:59 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: Connect two different database server using PHP


hm, as expected…

could you PM (private messaging) me both host names, so that I can try if I get the same errors?
  #17  
Old July 2nd, 2009, 11:04 AM
Member
 
Join Date: Sep 2008
Posts: 42

re: Connect two different database server using PHP


send me ur email id or how can i send PM via bytes
  #18  
Old July 2nd, 2009, 11:10 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: Connect two different database server using PHP


go to "User Control Panel" (click on your username just below the Bytes logo). in the left side menu there is the "Private Messages" group, click on "Send New Message". use "Dormilich" (w/o the quotes) as recipient. type message. click on the "Submit Message" button when you’re done. ;)

alternatively, click on my username, this brings you to my public profile. go to the "Contact Info" tab, click on "Send a Private Message …". proceed as above.
  #19  
Old July 2nd, 2009, 11:28 AM
Member
 
Join Date: Sep 2008
Posts: 42

re: Connect two different database server using PHP


i have sent you the message with all the details...

thanks
  #20  
Old July 2nd, 2009, 12:24 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: Connect two different database server using PHP


1) can't connect to second server (hostname (chessimmigration.com) unknown) but it should work for you since it's your localhost

2) can find first server, but can't establish connection
Expand|Select|Wrap|Line Numbers
  1. $c1 = mysql_connect('chessglobalproperties.com', $user, $pass);
  2.  
  3. Warning: mysql_connect(): Lost connection to MySQL server at 'reading initial communication packet', system error: 61
after some research I found out that "system error: 61" means that you can only connect this particular DB with "localhost" as host name (i.e. not from outside). you need to alter the mysql conf file to get this working.

note: to pass the socket you need to use MySQLi
  #21  
Old July 3rd, 2009, 05:26 AM
Member
 
Join Date: Sep 2008
Posts: 42

re: Connect two different database server using PHP


thank you very much for your co-operation...

i will do the same according to your reply...
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select data from two different databases krzys answers 3 November 11th, 2008 08:45 PM
IIS and Apache to serve one PHP page? sgottenyc@yahoo.com answers 8 August 7th, 2007 01:15 AM
Database Comparison? democratix answers 5 July 17th, 2005 09:39 AM
PHP & mysql Ken answers 1 July 17th, 2005 04:59 AM