In the context of PHP programming, what are the common pitfalls to avoid when dealing with multiple database connections in a single script?

When dealing with multiple database connections in a single script in PHP, common pitfalls to avoid include not closing connections properly, mixing up connection resources, and not handling errors effectively. To solve this, it's important to close connections after use, keep track of each connection resource separately, and implement error handling to catch any issues that may arise.

// Establishing multiple database connections
$connection1 = new mysqli('localhost', 'username1', 'password1', 'database1');
$connection2 = new mysqli('localhost', 'username2', 'password2', 'database2');

// Perform queries using each connection
$query1 = $connection1->query('SELECT * FROM table1');
$query2 = $connection2->query('SELECT * FROM table2');

// Close connections after use
$connection1->close();
$connection2->close();