What are the common pitfalls to avoid when working with MySQL connections in PHP scripts?

Common pitfalls to avoid when working with MySQL connections in PHP scripts include not closing connections properly after use, not sanitizing input data to prevent SQL injection attacks, and not handling errors effectively.

// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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

// Perform database operations

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