PHP - Access other web database - php

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?

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.

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.

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

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

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

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.

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

Related

API to connect to my MySQL Database Remotely

apparently my hosting provider does not support Remote MySQL Usage as it says in its Knowledge Base even though i bought a premium package
remote MySQL connections are disabled for security and performance reasons. You can only connect to MySQL from your PHP scripts hosted on our servers.
is there any way i can make an API so that i can connect to my MySQL Remotely ?. i need to use the Database in my Host Account as a source of information for my Android Application. thanks
You should look into using something like a HTTP Tunnel.
This post outlines a method of doing this for Android.
Basically you connect through this tunnel which is placed on your server, and can the communicate with the server as if you were localhost.
SSH is also another option, although you'll need remote SSH access enabled by your host. That's normally something you'll have to specifically request for them to enable.
You would then create an SSH tunnel using a technique like this and then use that as your connection for your database. Once you've initiated the connection you would then query it as normal.
There are possibilities here.
Your hosting provider may have allowed access of the database only
on certain IP(s) on certain PORTS. In this case, you cannot access
the database even if you write API's because the connection is not
open to the IP/PORT through which you are accessing through.
The database admin can also block access to certain table(s) or
database(s) for certain users.

Configure database connection to run web application anywhere

Using phpmyadmin as my database software I have created mobile web app that uses php to contact the database. I am using phpmyadmin through the university server. My problem is I don't no how I would go about connecting this database to my web application from anywhere in the world. For example if I’m sending a picture to the database via my web application it doesn't have the right connection to phpmyadmin to work outside of the university environment. Can anybody shed some light on how I would go about this in terms of how to connect to phpmyadmin, does it need different credentials from when I’m using it in the university, and below is an example of how it is currently connecting:
$conn = mysql_connect('localhost','USERNAME','PASSWORD') ;
$db = mysql_select_db('B00556019');
Should I be contacting this database differently, like for example changing the localhost to something else? Also can PHP be used to contact the university phpmyadmin database and have my web application working from it? And sorry for the lack of knowledge and detail I'm very much below par with databases. If you need any more details to help understand better let me know. Thank you for your time.
Change your connection code as below :
$conn = mysql_connect('Server ip address','username','password') ;
$db = mysql_select_db('B00556019');
YOu need to change localhost with your server ip.
Note :
Most default installs of MySQL only listen to the local machine.
Look for the bind-address setting in my.cnf on the server, and make sure it's listening on the IP address you're targetting. You may also need to ensure skip-networking isn't switched on!
Alternatively (and less securely!) the following will set it up to listen on all addresses - local and remote:
bind-address = 0.0.0.0
Create a webservice and have your mobile app send data to that. Far more secure.
Then the webservice could write them to the database (which could then be on localhost). Webservice will just take queries write them to the database and provide responses over http back to your mobile app.
You also won't get firewall issues as it will all be over HTTP then.

how to access database from one website to another website

Hi I'm having problem with accessing database from one of my site to another..
I have one site say a.com and I want to replicate the database from this site to my another site say b.com...
I'm using Cpanel in both the sites. In site A.com, I have created a user with required privileges and also added the other website (b.com) in the access hosts..
Now to access a.com database from b.com, I can use the username and password I have created on a.com but I'm not sure what hostname should I use..
Website a.com use localhost as the hostname..what should I use with b.com..
if you can't connect to the database then it's better to develop some kind of api. Try this
search api to use in external websites you might get some help.
Assuming:
the database is configured to be exposed on the network (if it is designed to be used only from the same machine it shouldn't be, UNIX sockets are preferred)
there is no firewall in the way
the database permissions table grants rights to users on the other server
… then it would be the ip address of the machine the database server is running on.
You should use a.com, or a decent IP address/domain name that will correctly direct the connection to the database.
However, do note you will need to configure the incoming server to accept the connection (i.e. port forwarding, firewall etc).
we can get data from another site by using $.getJSON function of jQuery
You can get detail information from
http://api.jquery.com/jQuery.getJSON sites....

Hyperedit, php and mysql

I know this is not a particular php question, but it is to do with accesing php by using mysql when the file is not hosted on the same domain.
For example, im using hyperedit, the php coding tool that allows you to display the results of php in realtime as you code.
However when i try and type my mysql code in, that i know works(i tested it using my servers built in php/html etc creator) i get a error:
Connection refused (trying to connect via tcp://myservername.servername.com
Its definitely not my code, i think instead it is something to do with the fact that the file is not being hosted on the server? I have only tried it in hyperedit but i think the same would occur in other programs too.
I know its not specifically php or coding related, but i thought this is the best place to ask!
If anyone could help me it would be great!
Thanks very much!
This is because your local development machine does not have access to the mysql server in question.
If this is a shared account, you may be able to grant access to your remote machine's IP/hostname through cPanel. If you have direct access to the remote box through the cli, you can do it via a sql GRANT command, like so:
GRANT SELECT ON mydb.* TO myusername#'my_IP' IDENTIFIED BY 'mypassword';
Otherwise, it may be locked down in a shared environment where only machines on the local subnet can access the mysql server (cough, GoDady, cough).
So there's no real answer. Either you have the privileges to give your remote machine access, or you don't. If you don't, consider using another host that does allow you to do what you need.

How can I connect from one website to database of another website in php

How can I connect from one website to database of another website in php.
The other website will require their port to be open for yours to connect to. They will also need the database user to allow connections that are not just from the local machine.
If you have a fairly standard LAMP set-up, this will mean opening port 3306 on the database server so outside connections can be made, and a database user setting up that will be able to connect from the outside world.
Bear in mind their may be latency issues.
You add the connection details in config.php or where you store them, and another-website.com or the IP to the host url (You probably have localhost there), then the username and the password.

Categories