What are the common errors or issues that may arise when trying to establish a connection to a database on a different server in PHP?

One common issue when trying to establish a connection to a database on a different server in PHP is incorrect database credentials. Make sure the hostname, username, password, and database name are all correct. Another issue could be that the remote MySQL server is not configured to accept connections from the IP address of the PHP server.

// Database credentials
$servername = "remote_server_hostname";
$username = "remote_server_username";
$password = "remote_server_password";
$database = "remote_database_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";