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:
- Only allocate resources (file handles, sockets, database connections, etc.) when your program is ready to use them.
- 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
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.
Is closing the mysql connection important?
From the documentation:
Note: The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close().
If your script has a fair amount of processing to perform after fetching the result and has retrieved the full result set, you definitely should close the connection. If you don't, there's a chance the MySQL server will reach it's connection limit when the web server is under heavy usage. If you can't close the MySQL connection until near the end of the script, it's cleaner though unnecessary to do so explicitly.
I'm not certain how fastcgi affects things. One page claims that a build of PHP that supports fastcgi will create persistent connections, even for mysql_connect. This contradicts the documentation in that the connection is closed when the process, rather than the script, ends. Rather than testing it, I'm going to recommend using mysql_close(). Actually, I recommend using PDO, if it's available.
When to close MySQL connections in PHP
No, you dont have to close your connection. As per the manual:
Using mysql_close() isn't usually necessary, as non-persistent open
links are automatically closed at the end of the script's execution.
See also freeing resources.
Most applications only have one database connections, so actually storing the connection resource isnt needed either.
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.
Where to Close MySQL Connection on PHP
You could change this part
$result = mysql_query($sql);
$title = mysql_result($result, 0,0);
echo trim($title);
to
$result = mysql_query($sql) or some_exception_function("ERROR IN QUERY:".$sql."<br>".mysql_error()); // here you can send an email with error, or whatever you want, if some error occurs
$row = mysql_fetch_array($result);
$title = $row['title']; // so you always fetch desired column by it's name
echo trim($title);
and like @fred-ii said, there is no need to close mysql connection
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
.
Related Topics
Using Ajax to Pass Variable to PHP and Retrieve Those Using Ajax Again
Adding a Namespace When Using Simplexmlelement
Belongstomany Relationship in Laravel Across Multiple Databases
How to Display PHP & HTML Source Code on a Page
Passing Variables from PHP to JavaScript
How to Pass Parameters into a PHP Script Through a Webpage
Wordpress: Loading Multiple Scripts with Enqueue
How to Refresh a Page When a Database Is Updated
How to Override Built-In PHP Function(S)
Is HTMLspecialchars Enough to Prevent an SQL Injection on a Variable Enclosed in Single Quotes