How can one access a MySQL database on a different server in PHP?

To access a MySQL database on a different server in PHP, you need to establish a connection to the remote server by specifying the host, username, password, and database name in the connection parameters. You can use the mysqli or PDO extension in PHP to connect to the remote MySQL server and execute queries.

// Connection parameters
$host = 'remote_server_ip';
$user = 'username';
$password = 'password';
$database = 'database_name';

// Establish connection
$mysqli = new mysqli($host, $user, $password, $database);

// Check connection
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Query example
$query = "SELECT * FROM table_name";
$result = $mysqli->query($query);

// Close connection
$mysqli->close();