Using MySQL_Close()

Do I really need to do mysql_close()

In most cases calling mysql_close will not make any difference, performance-wise. But it's always good practice to close out resources (file handles, open sockets, database connections, etc.) that your program is no longer using.

This is especially true if you're doing something that may potentially take a few seconds - for instance, reading and parsing data from a REST API. Since the API call is going over the line, negative network conditions can cause your script to block for several seconds. In this case, the appropriate time to open the database connection is after the REST call is completed and parsed.

To sum up my answer, the two big rules are:

  1. Only allocate resources (file handles, sockets, database connections, etc.) when your program is ready to use them.
  2. Free up resources immediately after your program is done with them.

What mysql_close() does exactly?

mysqli_close() closes the connection to the database for that specific page ONLY, there is nothing such as disconnecting the website from database which has to be connected again to work, a new connection is started on each page and the connection is automatically closed once the execution ends. mysqli_close() will close it before the execution ends, so to demonstrate this

<?php
// connection starts here
mysqli_query($link, "whatever stuff you need to do");
?>
// The connection is closed here

In another example of how mysqli_close is used

<?php
// connection starts here
mysqli_query($link, "whatever stuff you need to do");
mysqli_close($link); // the connection is closed here
// do whatever more stuff you have to do that are unrelated to the database
?>

As to exit, it's only use is breaking out of the code, there's no use in placing it at the end of the code

Using it this way below would be useless:

<?php
echo 'Hi';
echo 'Bye';
exit(); // this is useless because all code has been executed already
?>

The example below, is useful:

<?php
echo 'Hi';
exit(); // this is useful as it stops the execution of whatever code is below it.
echo 'Bye';
?>

Do I need to use mysql_close(connection)?

No, this won't help you if you close it at the end of the script. mysql_close() is just useful in case you want to free up resources before you end your script, because your connection is closed as soon as execution of the script ends

benefit of calling mysql_close()

None if you have it as the last line of the script.

You can improve performance by closing as soon as you are done so the handle is available for other processes to use.

This implies you are doing some other stuff after you are done with the db.

PHP mysql_close() and mysql_free_result() - at what point should I be using them?

If the scripts are running for a very short time, like to deliver a web page, it's alright not to call mysql_close and mysql_free_result, since PHP will close the connection and free the memory automatically at the end of your web page execution.

On the other hand, if you'll write a long running batch script, doing thousands of query, it may be a good idea to call mysql_free_result.

Are mysql_close and pg_close required?

If you don't call it, the socket connection to the database remains open for ~30 seconds in a wait state.

If you get lots and lots of people and you don't somehow manage to reuse these zombie connections, your database might explode with a too many users error.

So in answer to your question: syntactically not required but it's very poor practice not to include them.



Related Topics



Leave a reply



Submit