What are the common pitfalls to avoid when handling database connections in PHP?

One common pitfall when handling database connections in PHP is not properly closing the connection after use, which can lead to resource leaks and potential performance issues. To avoid this, always close the database connection once you are done using it. Another pitfall is not properly sanitizing user input before using it in database queries, which can leave your application vulnerable to SQL injection attacks. Always use prepared statements or parameterized queries to prevent this.

// Open a database connection
$connection = new mysqli($servername, $username, $password, $dbname);

// Perform database operations

// Close the database connection
$connection->close();