I want to explicitly close mysql connection in my php script to prevent too many connections, using below code:
<?php
$db = new PDO('mysql:host=HOST;dbname=DB',USER,PASSWORD);
$sth=$db->query("SHOW TABLES");
$sth=null;
$db = null;
sleep(10);
?>
Without adding $sth=null; in the above code, I am not able to close my mysql connection.
As stated in PDO document,
To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted
To ensure above, by assigning NULL to the variable that holds the object should ideally close my connection. but for the same, I need to destroy pdo statement handler references also.
In my code, I can make null all references of PDO object but to destroy statement handler in the large codebase is very difficult task for me. Any workaround?