What are the potential pitfalls of using multiple database connections in a PHP script?

When using multiple database connections in a PHP script, potential pitfalls include increased complexity, potential for errors in managing multiple connections, and potential performance issues. To mitigate these risks, it is recommended to use a database connection pooling mechanism or a database abstraction layer to handle multiple connections efficiently.

// Example of using a database abstraction layer to handle multiple connections efficiently

// Create a database abstraction layer class
class Database {
    private static $connections = [];

    public static function getConnection($dbname) {
        if (!isset(self::$connections[$dbname])) {
            self::$connections[$dbname] = new PDO("mysql:host=localhost;dbname=$dbname", 'username', 'password');
        }
        return self::$connections[$dbname];
    }
}

// Example usage
$connection1 = Database::getConnection('database1');
$connection2 = Database::getConnection('database2');