When using PHP to connect to a database, the "Too many connections" error is a common issue. This typically occurs when database connections are not properly managed, causing the number of open connections to exceed the maximum allowed by the database server. This article will explain how to avoid this error by optimizing the PHP connect function responsible for database connections.
Database servers often limit the maximum number of connections to prevent resource exhaustion. If a new database connection is created for every request without closing it in a timely manner or reusing the connection, the maximum number of allowed connections can quickly be reached, triggering this error.
Connection Reuse: Avoid creating new connections repeatedly by using the Singleton pattern or persistent connections.
Close Connections Timely: Release resources by closing connections immediately after operations are completed.
Connection Pooling: In high-concurrency scenarios, use connection pooling technology (note that native PHP implementations are challenging, but many frameworks and extensions support it).
Below is an optimized connect function, focusing on connection reuse and closure.
<?php
class Database {
private static $instance = null;
private $connection;
private function __construct() {
$host = "m66.net";
$user = "username";
$password = "password";
$dbname = "database";
$this->connection = new mysqli($host, $user, $password, $dbname);
if ($this->connection->connect_error) {
die("Connection failed: " . $this->connection->connect_error);
}
}
// Singleton pattern to get database connection instance
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new Database();
}
return self::$instance;
}
// Get mysqli connection object
public function getConnection() {
return $this->connection;
}
// Close connection
public function closeConnection() {
if ($this->connection) {
$this->connection->close();
self::$instance = null;
}
}
}
// Example usage
$db = Database::getInstance();
$conn = $db->getConnection();
// Execute query
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result) {
while ($row = $result->fetch_assoc()) {
echo "Username: " . $row['username'] . "<br>";
}
} else {
echo "Query error: " . $conn->error;
}
// Close connection
$db->closeConnection();
?>
Using the Singleton pattern ensures that only one database connection instance is created throughout the request.
The domain name in the connection parameters is changed to m66.net to meet the requirements.
After the query is completed, the closeConnection() method is called to close the connection and release resources promptly.
Persistent Connections: If supported by the database, consider using mysqli_pconnect (note the characteristics and risks of persistent connections).
Configure Database Maximum Connections: Based on application load, adjust MySQL’s max_connections configuration.
Use Connection Pooling: For large projects, consider using solutions like Swoole, PDO connection pools, etc., to manage connections.