问题
Do I need to explicitly kill and/or close a mysqli connection at the end of a script. At the moment, I do neither, and it works, but I don't have much load.
I've heard that there is connection pooling, and mysqli connections are re-used... do I need to close the connection then at all?
回答1:
You should always close your connections at the end of the main script. If you uses some php5 and object to handle your DB just think about using __destruct()
to automatically close it
http://php.net/manual/en/language.oop5.decon.php
回答2:
No it is not necessary to close a mysql connection at the end of a script. PHP does it automatically.
回答3:
it is not necessary but generally that's good practice. I personally, when wish to boost error-checking mechanism, you $con->close() for almost each function. e.g:
if($stmt = $db->prepare($statment))
{
if($db->bind_param($str, $param1, $param2))
{
// the rest of the code and error checking
}
else
}
$db->close();
}
}
else
{
$db->close();
}
This makes OOP necessary. Since you can simply write function doing this automatically with many other security checks added. codeIgniter as a framework has nice DB connectors whith superb security at a basic general level.
来源:https://stackoverflow.com/questions/5232458/close-and-or-kill-mysqli-connection-at-the-end-of-the-php-script