deka
deka

Reputation: 409

PHP - Access other web database

Newbie question. I have a project that need to access other web database using PHP. Is it possible?

for example: I have a domain www.domain1.com with database1 and I have another domain www.domain2.com with database2 with different hosting. Can I access database1 from www.domain2.com which is also using database2?

Is it possible if it is the same hosting with 2 different domain?

Upvotes: 0

Views: 368

Answers (7)

Soundz
Soundz

Reputation: 1300

if you can access other stuff outside your localhost you can just like this:

$conn2 = mysql_connect($otherhost, $otheruser, $otherpassword, true);
//the true at the end makes a new link

$query = mysql_query($your_query, $conn2); 

this just send a query to the other database

Upvotes: 0

gd1
gd1

Reputation: 11403

Yes. In order to do that, just follow the other answers here.

But, in order to do things the right way, it is more recommendable to create some middleware between your web application and the remote database (e.g. SOAP, RESTful, or just raw ad-hoc XML). It will add security and decoupling.

Upvotes: 1

zomboble
zomboble

Reputation: 833

Yeah as long as you have a port open 80 should do, make sure you let the host know so they can allow you an account through.

Upvotes: 0

Henrik Karlsson
Henrik Karlsson

Reputation: 5713

Yeah, if you use MySql you have to set it to allow remote connections and then pass the ip/domain of the remote db in the mysql connect function

Upvotes: 0

Keval Domadia
Keval Domadia

Reputation: 4763

You can connect to as many databases as many your application requires.

Get started with:

http://dev.mysql.com/doc/refman/5.0/en/connecting.html

Make sure port of domain2 allows authentication from domain1 (or you can keep it open for all, NOT RECOMMENDED).

If you are saying about JOINING databases then, so far I have worked on many projects where we had multiple databases on same server. So HOST was not an issue.

Upvotes: 0

Mihai Iorga
Mihai Iorga

Reputation: 39704

It is possible if

  • the remote database user is accepting connections from local script
  • the mysql port 3306 from machine2 is open for machine1

Upvotes: 0

Raptor
Raptor

Reputation: 54212

Yes, as long as the server port is open for PHP hosting domain .

Upvotes: 0

Related Questions