What are some best practices for managing multiple MySQL connections in a PHP project?

When managing multiple MySQL connections in a PHP project, it is important to establish a connection pool to efficiently handle and reuse connections. This can help improve performance and reduce the overhead of establishing new connections for each request. Additionally, using a connection manager class can help centralize the management of connections and ensure proper handling of connections throughout the project.

// ConnectionManager.php

class ConnectionManager {
    private static $connections = [];

    public static function getConnection($host, $username, $password, $database) {
        $key = $host . $username . $database;

        if (!isset(self::$connections[$key])) {
            self::$connections[$key] = new mysqli($host, $username, $password, $database);
        }

        return self::$connections[$key];
    }
}

// Example of using the ConnectionManager
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'my_database';

$connection = ConnectionManager::getConnection($host, $username, $password, $database);

// Use $connection for executing queries