How can PHP developers ensure secure and reliable connections to MySQL databases in their scripts?

PHP developers can ensure secure and reliable connections to MySQL databases by using prepared statements to prevent SQL injection attacks and by using SSL encryption to secure the connection. Additionally, developers should always sanitize user input and validate data before executing queries to prevent unauthorized access to the database.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

// Create a secure connection to the MySQL database
$conn = new mysqli($servername, $username, $password, $dbname);

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