What are the potential security risks of connecting to an external MySQL server in PHP applications?

Potential security risks of connecting to an external MySQL server in PHP applications include SQL injection attacks, data interception, and unauthorized access to the database. To mitigate these risks, it is important to properly sanitize user input, use prepared statements, and secure the connection to the MySQL server.

// Establishing a secure connection to an external MySQL server
$servername = "external-server.com";
$username = "username";
$password = "password";
$dbname = "database";

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

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