Some web projects are required accessing multiple databases on the different server. In that case, you should connect to the remote database from another server. For security reason remote access to MySQL database server is disabled. You need to enable remote MySQL access to connect MySQL database from the different server.
Remote access will allow you access MySQL database from another server. This access is helpful when you want to connect multiple databases hosted on different server. This tutorial explains how to connect to the remote MySQL database using PHP.
Assume that the MySQL database is hosted on Server A and you want to connect this database from Server B using PHP script. So, the remote database connection process would be the following.
<?php
$dbServerName = "example.com";
$dbUsername = "exdbuser";
$dbPassword = "exdbpass";
$dbName = "codexworld";
// create connection
$conn = new mysqli($dbServerName, $dbUsername, $dbPassword, $dbName);
// check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
users
table to the remote database.
<?php
/*
* get data from remote database table
*/
$sql = "SELECT id, name FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
?>
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Php running on phpmyadmin ( it has dynamic IP) and in this case if I want to connect multiple IPs to MySQL. What I have to do.
Php script running on localhost ( it has dynamic IP) and in this case if I want to connect local PHP script to MySQL. What I have to do.
thanks
thanks!