Closing PHP MySQL Connections

The old mysql_close() function closed a connection created by the removed original MySQL extension. Current applications use PDO or MySQLi instead.

Historical mysql_close()

<?php
// Historical only: removed from PHP 7+
mysql_close($link);
?>

Closing a PDO connection

A normal PDO connection is released when its object is destroyed, typically at script end. You can release it earlier by removing the reference:

<?php
$dbo = null;
?>

Closing a MySQLi connection

<?php
$connection->close();
?>

Connection, query and cleanup example

<?php
$dbo = new PDO(
    'mysql:host=localhost;dbname=sql_tutorial;charset=utf8mb4',
    'userid',
    'password',
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

$total = $dbo->query('SELECT COUNT(*) FROM student')->fetchColumn();
echo 'Total records in Student table = ' . $total;

$dbo = null;
?>

Explicitly closing a normal connection is optional when the script is about to end. See PHP/MySQL connections and executing queries.

← All PHP & MySQL topics




Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer