Remote SQL connection in php - php

I have a MySQL database on my website, say example1.com . I want to access a table of this database from my other website, say example2.com . I have added example2.com in the remote MySQL access users list.
What else will I need to do to achieve this ? Can anyone help me with an example.

You need to use the database domain as the host of your database connection and make the ip of the other website trusted for the first website host, or to have access via ssh, then you can connect ^^.

Related

Connecting to remote Mysql databases from Php scripts

I have two websites (managed separately with Cpanel). I want to use a database on one host from another.
Domains and their mysql ports are: abc.com:2082 and xyz.com:2082.
I would like to use the database on abc.com from a Php script running on xyz.com. However when I try to connect to the database, the connection is showing an error: "Error".
Well, You can do it in two ways first connecting database using database credential. in that case, you need to enable mysql remote permission . if provide % then anyone from any ip address can connect with your database.
Secondly, You can use restful api to exchange data in between. to know more about restful api search in the stackoverflow.

Connecting to database as a remote user

I know that you can access a database from a domain name with a user like this user#example.com but will this user also be able to access the database if they are trying to connect via another page on that domain i.e. example.com/page?
I am asking this as I am trying to connect from a domain name that will be establishing connections from different pages and sub folders i.e. example.com/admin/page, will setting it up with just example.com as the ending will all variables of that domain be accepted from the database connection or would each page need to be set up as seperate users on the database?
EDIT:
when I say "I know that you can access a database from a domain name with a user like this user#example.com" I mean that you can create a remote mysql user that can access from a domain name, the statement in mysql would look like GRANT ALL PRIVILEGES ON *.* TO user#example.com IDENTIFIED BY 'password' WITH GRANT OPTION; what im asking is, can this user access from all variables of example.com?
I think you are confusing a few concepts here.
The "page" as in webpage, or domain as in www.example.com are of little concern when connecting to a database.
You seem to be using mysql, so what you need to do is make sure your process that handles some web request (eg your apache server, or IIS, or whatever) can connect to the database.
eg:
If you apacheserver is running on 1.2.3.4, make sure your mysql allows connections from 1.2.3.4
The webpages themself are not important.
Yes users from different pages will be able to connect.
No you do not need to create different users for each url in your application.
The domain part of the user name is determined by MySQL doing a DNS resolution on the connecting IP. It has nothing to do with the page request to your PHP script.

Connect MySQL database to several domains

I have two websites with different domains and I have to share data between the two, such as user registered on first website will be login on the second website too.
Is it possible to connect the second website to the first website's MySQL database? I.e. I have two websites www.example1.com and www.example2.com. Is it possible for example2.com to connect to database of example1.com?
I use the following PHP code which works fine for example1.com but not on example2.com:
$conn =mysql_connect("HOST","USERNAME","PASS") or die("cannot connect with db").mysql_error();
$dbname=mysql_select_db("DATABASE_NAME") or die("Error selecting db").mysql_error();
On example2.com, the connection fails with error message
cannot connect with db....
Could you please explain me what is the configuration required for such type of connection and what are the things which may stop this connection?
if the 2 domains are hosted in the same server then no problem at it all you may connect with the database with as much scripts as you want .
if they are in different servers from your database server you can do this but you have allow remote connection for each script IP
It depends on where you host your database and your website. Some webhostings allow only local database connection. So make sure that either both websites are on the same server or that your settings allow you to connect from your PHP code to the remote database and the database allows remote connection.
If you have command-line access to your webserver, check that you can connect using mysql command - see this answer.

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....

Connecting to a remote database from a localhost computer using mysql and PHP

So here is the situation.
I have a database on a remote server that gives out quizzes and scores for individual students...
I also have a local database which contains the students names and their respective groups.
What I want to do is to display the list of students per group then show the results of their scores from the remote database.. Is this possible??
Im currently running the script from my local computer using XAMMP like so
mysql_connect("REMOTE SERVER ADDRESS","USERNAME", "PASSWORD") or die("Could not connect to MySQL server!");
But all I get is Could not connect to MySql Server..
GRANT ALL ON database.* TO user#ipaddress IDENTIFIED BY 'password';
You should force a reload of the grant tables using:
FLUSH PRIVILEGES;
https://documentation.cpanel.net/display/ALD/Remote+MySQL# I found useful this one myselt. When you haven't define access permissions to remote db using it's cpanel, then you cannot connect with it via the localhost from your PC.
You will find something like 'Add Access Host' in your hosting server cpanel and there you can add which IPs are you going to use to access from outside. There I used as "%.%.%.%" and that means I allowed every v4 IPs to access my database. Instead of "%.%.%.%" you can put your PC's IP(for example 51.254.230.178). As PC's IP gets changed frequently, then I used this because I cannot change it manually all the time by checking my IP every time I connected to the Internet.

Categories