When to call mysqli :: close

When do I need to call mysqli :: close? I never used if the instructions for checking the success of bind_param (), prep (), and execute (). Should I call $ stmt-> close () at the end of the method (see below). Or I should call it after each condition, ensuring that I close the database connection, even if the process fails at some stage, for example, bind param.

public function function_name($id,$new_id ){ $query = "UPDATE TABLE SET name = ? WHERE field = ? "; if($stmt=$this->prepare($query)){ if($stmt->bind_param("is", $id, $new_id)){ if($stmt->execute()){ }else{//Could not execute the prepared statement $message = "Could not execute the prepared statement"; } }else{//Could not bind the parameters $message = "Could not bind the parameters"; } }else{ $message = "Could not prepare the statement"; } return $message } 
+4
source share
4 answers

When PHP exits, it gracefully closes the database connections.

The only reason to use the close method is when you want to end the connection to a database that will no longer be used, and you have a lot to do: how to process and stream data, but if it's fast, you can forget about the closed application.

Putting it at the end of a script means redundancy, lack of performance, or increased memory.

What is important: delete unused data, and if you want to avoid memory leaks (which in my humble treatment are a PHP core problem in this case), use:

 mysqli_kill(); mysqli_close(); 

Thus, the socket is also killed.

+7
source

You should always gracefully close the connection when you are done with it to avoid performance issues and memory / leakage. On the other hand, you also need to make sure that you really coped with it - your script will fail if you try to use a closed connection.

The same applies to operators. If the instructions will no longer be used, dispose of them accordingly.

0
source

Just thought you could create another method called function_close and call it after calling the function_name method.

That way, if you switch to pdo or mongo, you just have to reorganize the methods, not every instance of close .

0
source

you should close it, especially after a multiprocessor request ( http://php.net/manual/en/mysqli.multi-query.php ), as you may get memory problems. Otherwise, closing is not required.

0
source

Source: https://habr.com/ru/post/1394021/


All Articles