Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use mysqli_kill() and mysqli_close()?

I have my own database which I'm running using Xampp. I'm using the Apache and MySql services on it, though I given public access just yet. I would like to know the proper way to end a bunch of sql queries. I'm using ajax to send data to a php page and query some data from a server. During the ajax requests the I programmed the handler php page so it will be performing multiple queries (at least more than one) to get the specific data I need. Now, I don't know when in the process I should use mysqli_kill() or mysqli_close(). I have it set up right now so only after all the immediate queries for that particular ajax request have been performed, then run mysqli_close(). I don't even use mysqli_kill(). I don't know too much about database connections or managing performance on a server so I don't know how to consider this. On one hand, I don't need to close the connection until that user logs off the webpage, right? Or should I just open and close each connection as it comes and is processed?

This handler php page processes requests from a few webpages I have and in one of the webpages there is an input box that sends ajax requests every keystroke to show hints (possible matches)... also note this is an employee access only page so connections shouldn't be too high.

I also took a look at this post Could someone explain a little about this statement about mysqli close function?

The answer is contradictory to the suggestion taken from the php manual website. Would someone please clarify when to use mysqli_kill() and mysqli_close() as well if, for the circumstances I described, there would be any reason to keep a connection alive for the sake of performance?

like image 762
Klik Avatar asked Jan 15 '23 06:01

Klik


1 Answers

In general, you do not need to use either of these functions at all. All resources that the server has allocated to you will be released automatically when the PHP script finishes executing, so in the most common scenario there is nothing you should be concerned about.

Sometimes you might want to use mysqli_close, for example inside a long-running script that has done what it needs to do with the database and does not wish to tie up server resources for the remainder of its execution.

You should never have need to use mysqli_kill at all: what this function does is instruct the server to destroy a worker thread. This is getting into "mucking around where you are not invited" territory, which might be useful in some situations but certainly never under normal operation.

like image 110
Jon Avatar answered Jan 17 '23 18:01

Jon