Current Location: Home> Latest Articles> How to Gracefully Close the connect Function Connection in PHP to Avoid Resource Leaks and Performance Issues?

How to Gracefully Close the connect Function Connection in PHP to Avoid Resource Leaks and Performance Issues?

M66 2025-07-29

In PHP development, managing connection resources properly is crucial when connecting to databases or other services. In particular, connections established using the connect function, if not closed properly, can lead to resource leaks, which in turn may impact application performance and stability. This article will explain in detail how to gracefully close the connect function connection in PHP to avoid potential resource and performance issues.


1. What is Resource Leakage and Its Dangers?

Resource leakage refers to the failure of a program to release system resources (such as database connections, file handles, or network connections) after they are no longer in use, leading to resource exhaustion. For PHP, if a connection is not closed, it will occupy memory and system connection limits, causing subsequent requests to fail when trying to establish new connections, eventually leading to performance bottlenecks or system crashes.


2. Common connect Connection Methods in PHP

In PHP, common connection functions include:

  • mysqli_connect() — Used for MySQL database connections.

  • pg_connect() — Used for PostgreSQL database connections.

  • Custom connect functions — These may be used for connections to specific APIs or services.

Regardless of the type of connection, it is necessary to explicitly or implicitly close the connection.


3. How to Gracefully Close Connections?

3.1 Example Using mysqli

<?php
$host = "m66.net";
$user = "username";
$password = "password";
$dbname = "test_db";
<p>// Establish connection<br>
$conn = mysqli_connect($host, $user, $password, $dbname);</p>
<p>if (!$conn) {<br>
die("Connection failed: " . mysqli_connect_error());<br>
}</p>
<p>// Execute query<br>
$sql = "SELECT * FROM users";<br>
$result = mysqli_query($conn, $sql);</p>
<p>// Process results<br>
if ($result) {<br>
while ($row = mysqli_fetch_assoc($result)) {<br>
echo "Username: " . $row['username'] . "<br>";<br>
}<br>
// Free result set<br>
mysqli_free_result($result);<br>
}</p>
<p>// Close connection<br>
mysqli_close($conn);<br>
?><br>

The key points here are:

  • After using the database query results, call mysqli_free_result() to free the result set resources.

  • After using the connection, call mysqli_close() to close the database connection and release resources.

3.2 Example Using PDO

PDO automatically closes the connection when the object is destroyed, but explicitly disconnecting is clearer:

<?php
$dsn = "mysql:host=m66.net;dbname=test_db;charset=utf8mb4";
$username = "username";
$password = "password";
<p>try {<br>
$pdo = new PDO($dsn, $username, $password);<br>
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);</p>

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo "Username: " . $row['username'] . "<br>";
}

// Close connection
$pdo = null;

} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>

Setting $pdo to null will close the connection and release resources.


4. Issues Caused by Unclosed Connections

  • Resource Exhaustion: As the number of connections increases, once the server limit is reached, new connection requests will be denied.

  • Performance Decline: Increased memory usage can cause slower responses.

  • Security Risks: Unreleased connections may exhaust the connection pool, affecting system stability.


5. Summary

  • Never overlook the importance of closing connections.

  • Use built-in functions like mysqli_close() or set the connection to null to ensure it is closed.

  • Release result set resources after processing the results.

  • For custom connect functions, ensure the documentation provides an interface to close the connection and call it in the program.

By properly managing connection resources, you can improve application performance and ensure stable system operation.