What are some potential pitfalls to watch out for when establishing database connections in PHP scripts, especially when dealing with different hosting providers?

One potential pitfall when establishing database connections in PHP scripts is that different hosting providers may have different configurations or restrictions that could cause connection failures. To avoid this, it's important to use the correct credentials, host, and port information provided by your hosting provider. Additionally, always handle connection errors gracefully by using try-catch blocks to catch exceptions and provide appropriate error messages to the user.

<?php
$host = "your_host";
$username = "your_username";
$password = "your_password";
$database = "your_database";

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