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.
Related
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.
I know how to connect to a MySQL DB on a remote server. What I want to do is, connect to the remote mysql server on the clients site without them being able to see my connection credentials by simply downloading the sqlconnect.php file.
the reason for this is because I have built multiple sites that are almost identical. they are essentially re-seller sites. I built it in a way that one database handles all the sites and simple "site ID's" passed on by each site determine the content delivered.
I need to connect to Mysql remotely but dont want to leave the connection file on each persons server. I'm not so much interested in protecting the rest of the code, just the mysql connection credentials itself.
I want to run two sites using single database one cpanel-phpmyadmin how can i access to another site to that same database uploaded on one site.
for 1st site i use code for connect to database
mysql_connect("localhost","User","password");
mysql_select_db("database");
what will be the database connection code in php so that i can assess one database to multipal sites...
At first you should check your MySQL settings to make sure that it allows remote connections. If remote connections are allowed, from remote server you can connect in a similar way by specifying your current server's IP address instead of "localhost" where database is running, i.e. if database is running on the server with IP address 123.123.123.123, then the code will look like:
mysql_connect("123.123.123.123","User","password");
mysql_select_db("database");
At first you should check your MySQL settings to make sure that it allows remote connections. If remote connections are allowed, from remote server you can connect in a similar way by specifying your current server's IP address instead of "localhost" where database is running, i.e. if database is running on the server with IP address 123.123.123.123, then the code will look like:
mysql_connect("123.123.123.123","User","password");
mysql_select_db("database");
you can try this
How can I connect my localhost PHP files to my remote server MYSQL database?
For example: if we download WordPress, and it gives us an option to login, so that our login credentials are saved with WordPress but still our localhost files connects to that database.
How do I do that?
Thanks.
If you want your PHP code to connect to a remote MySQL database, you just have to specify that remote host when calling the function that connects to MySQL.
For example, when calling mysql_connect(), don't specify localhost as first parameter -- but your real remote host.
Same with mysqli::__construct() or PDO, of course.
A couple of notes :
Your remote host must accept connections to MySQL (think about the firewall)
Your remote MySQL database server must accept connections from non-localhost
Sending queries, and fetching results, to/from a far away server will be a lot slower than doing so on localhost !
mysql_connect(SQL_IP, SQL_NAME, SQL_PASS);
Well when you install Wordpress set the host to be the IP of your remote server. If this isn't what you're looking for, please reword your question. If you're not using word press, check ou the PHP documentation for mysql_connect. The first parameter is the host. Enter the IP or hostname of your remote server here. And then follow that with your username and password.
mysql_connect('remote-host', 'myuser', 'mypassword');
Note: Some hosts do not allow remote connections to MySQL. Check your remote server doesn't have 3306 firewalled or only instructed MySQL to bind it to 127.0.0.1.
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.