How can developers ensure secure and reliable database connections in PHP applications hosted on external servers like 1&1?

To ensure secure and reliable database connections in PHP applications hosted on external servers like 1&1, developers should use secure connection methods like PDO with prepared statements to prevent SQL injection attacks. Additionally, developers should store database credentials securely and avoid exposing them in the code.

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

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>