What are common pitfalls when trying to establish a connection to a SQL server using PHP?

Common pitfalls when trying to establish a connection to a SQL server using PHP include incorrect server credentials, firewall issues blocking the connection, and missing required PHP extensions like PDO or mysqli.

// Establishing a connection to a SQL server using PHP with PDO
$servername = "localhost";
$username = "root";
$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();
}