You don’t need a close() method. You just set the variable to null.
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
// ... do your work ...
$db = null;
?>
That’s it. The connection drops.
Why there is no close() method
I used to look for a $db->close() call every time I finished a query. It feels logical. You open a resource, you close it. But PDO doesn’t work that way.
PDO relies on PHP’s garbage collector. When the script ends, PHP cleans up all objects that are no longer referenced. If your $db variable goes out of scope, or if you explicitly set it to null, the object is destroyed. When the object is destroyed, the underlying database connection closes.
In a standard web request, this is usually good enough. Your script runs, you get your data, you send the HTML, and the process dies. The connection closes automatically. You don’t need to do anything special.
The trap with PDOStatement objects
Here is where people get stuck. Setting $db = null doesn’t always close the connection immediately.
PDO holds the connection open as long as there are active references to it. This includes PDOStatement objects. If you have a prepared statement that hasn’t been released, the connection stays alive even if you null out the PDO instance.
Don’t do this:
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$stmt = $db->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([$id]);
// I think I'm done
$db = null;
// But the connection is still open because $stmt exists!
$stmt = null;
// Now it's actually closed.
?>
You need to clear the statements too. If you’re using fetch() in a loop, make sure you aren’t holding onto the result set in a way that keeps the statement alive. In most simple cases, just setting both $db and $stmt to null at the end of your block handles it.
unset() works too
I see people use unset($db) instead of $db = null. It does the same thing. Both remove the variable from the current scope, triggering the garbage collection.
Pick one and stick with it. I prefer $db = null because it’s explicit about intent. unset feels like “remove this variable.” $db = null feels like “release this resource.” For code that other people might read, that distinction matters more than the implementation details.
When you actually need to close it manually
If you’re writing a standard web app, you probably don’t care about this. The script ends, the connection drops. Done.
But I’ve written queue workers and cron jobs before. In those cases, the script doesn’t end until you tell it to. If you open a database connection, do some work, then spend thirty seconds calling an external API, you are holding a MySQL connection slot open for thirty seconds doing nothing.
If you have multiple workers running, or if your app is under load, you can hit max_connections. Your new requests will be refused with a “Too many connections” error. It feels like the database is slow. It’s not. You’re just hoarding connections.
In those scenarios, close it explicitly before the slow part:
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$data = getFromDb($db);
$db = null; // release the slot now
$response = callExternalApi($data); // slow step
?>
This keeps your connection pool free for other requests while you wait on the network.
How to verify it worked
Don’t guess. Check.
Open another terminal and connect to your MySQL instance. Run SHOW PROCESSLIST;. You’ll see a row for your PHP process. Now, trigger your script that closes the connection properly. Refresh that list. The row should be gone.
If it’s still there, you have a reference you missed. Usually it’s a statement object or a result set that hasn’t been cleared.
One exception: Persistent Connections
If you enabled persistent connections with the PDO::ATTR_PERSISTENT option in the constructor, setting the variable to null does not close the underlying socket. It just puts it back in the pool for the next request to use.
This is a different topic entirely, and it changes how you think about connection lifecycle. I’ve written more about persistent connections in PDO if you’re going down that road. For everything else, null is your friend.