Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqli - Do I really need to do $result->close(); & $mysqli->close();?

Tags:

php

mysql

mysqli

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?

like image 692
James Avatar asked May 21 '10 04:05

James


People also ask

Is MySQLi close necessary?

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.

Is it necessary to close connection in PHP?

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.

What is the purpose of close () in PHP *?

The close() / mysqli_close() function closes a previously opened database connection.

What is MySQLi -> Real_connect?

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.


1 Answers

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

like image 160
Emil Vikström Avatar answered Sep 22 '22 06:09

Emil Vikström