Just started using mysqli. If I'm working with small data sets on small websites (traffic-wise), do I really need to use these all the time?
$result->close(); $mysqli->close();
Also, for someone doing custom PHP and MySQL work without a framework, is mysqli the general preferred way of interacting with MySQL?
Explicitly closing open connections and freeing result sets is optional. However, it's a good idea to close the connection as soon as the script finishes performing all of its database operations, if it still has a lot of processing to do after getting the results.
Originally Answered: Is it necessary to close an MySQL connection in PHP? No (mostly). According to the documentation , database connections are closed at the end of a PHP script for non-persistent connections. MySQL 5.3 introduced persistent connections , which should be closed when you are finished with them.
The close() / mysqli_close() function closes a previously opened database connection.
Definition and Usage. The real_connect() / mysqli_real_connect() function opens a new connection to the MySQL server. This function differs from connect() in the following ways: real_connect() requires a valid object created by init() real_connect() can be used with options() to set different options for the connection.
PHP will close all open files and DB connections at the end of the script. It's good practice to do it manually when you are done with the connections, but it's no disaster if you don't. If you have a DB connection that will be used throughout the whole script you can as well leave it open.
+1 on PDO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With