What are common pitfalls when establishing a database connection in PHP scripts?

Common pitfalls when establishing a database connection in PHP scripts include using incorrect credentials, not handling connection errors properly, and not closing the connection after use. To solve these issues, always double-check the database credentials, use try-catch blocks to handle connection errors, and close the connection using the `close()` method or by setting the connection variable to null.

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

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

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

// Do something with the database

// Close the connection
$conn->close();
?>