How can the code snippet provided be improved for better functionality?

The code snippet provided lacks proper error handling for the database connection. To improve functionality, we can add error checking to ensure the connection is successful before proceeding with queries. This can be achieved by using try-catch blocks to catch any exceptions that may occur during the connection process.

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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();
}