What happens if MySQL connections continually aren't closed on PHP pages? - php

At the beginning of each PHP page I open up the connection to MySQL, use it throughout the page and close it at the end of the page. However, I often redirect in the middle of the page to another page and so in those cases the connection does not be closed. I understand that this is not bad for performance of the web server since PHP automatically closes all MySQL connections at the end of each page anyway. Are there any other issues here to keep in mind, or is it really true that you don't have to worry about closing your database connections in PHP?
$mysqli = new mysqli("localhost", "root", "", "test");
...do stuff, perhaps redirect to another page...
$mysqli->close();

From: http://us3.php.net/manual/en/mysqli.close.php
"Open connections (and similar resources) are automatically destroyed at the end of script execution. However, you should still close or free all connections, result sets and statement handles as soon as they are no longer required. This will help return resources to PHP and MySQL faster."

Just because you redirect doesn't mean the script stops executing. A redirect is just a header being sent. If you don't exit() right after, the rest of your script will continue running. When the script does finish running, it will close off all open connections (or release them back to the pool if you're using persistent connections). Don't worry about it.

There might be a limit of how many connections can be open at once, so if you have many user you might run out of SQL connections. In effect, users will see SQL errors instead of nice web pages.
It's better to open a connection to read data, then close it, then display data and once the user clicks "submit" you open another connection and then submit all changes.

Related

When does PHP call Write method on Session Handler Class?

I have this issue:
new MySqli
new Session - with the mysqli as parameter
$_SESSION Usage
close MySqli
But at the end, after I close MySqli, Session::_write is called. Can anyone tell me how can I securely close my MySqli connection after everything is done? Or I don't actually need to? I was thinking that every time the page is accessed a new MySqli connection is opened and never closed, which brings me to thinking I might get some kind of overflow sometime. Any Information would be much appreciated.
The PHP parser is only running when there is a script to process. How does it know when a script needs to be processed? Your web server sends them to it and outputs its data via http to the client. Every time the php process ends (end of script) it disconnects from the sql server, unless you've specified it to leave the connection open.
I would not be worried about it leaving the connection open due to the last Session::_write call, though I would call Session::write_close before closing the SQL connection if you are using it for sessions just to be safe and make sure all data is written back to the db.

detecting open database connections

I've built a website and has been running for a while now. but suddenly today I received and error that the maximum connections to the DB is reached.
how can I list the open connections and terminate them using PHP order even through the Db manegement server
This problem happens because you have a lot of users opening connections at the same time. Also, Ajax can cause this problem. Once the execution of the page is done, the connection should of been terminated. Make sure you are recycling a single connection per user every time the page opens when you try different things. For instance you do not want to reopen a connection to the DB (passing user and pass) every time you execute a DB query. Open it once and keep using it until the page renders. It could be that your connections are not being terminated properly but I have not run into this problem before; I do know that PHP has the potential to miss manage connections. Contact your host and ask how many connections are allowed to the DB at any one moment and if they can increase it.
Just every time after executing query close(); connection.
mysql_close ([ resource $link_identifier = NULL ] );
mysqli::close ( void );
mysqli_close ( mysqli $link );
The first method is to use PHP as a CGI "wrapper". When run this way, an instance of the PHP interpreter is created and destroyed for every page request (for a PHP page) to your web server. Because it is destroyed after every request, any resources that it acquires.

singleton does it work for my website

I have website which uses database, I have implemented singleton pattern to connect to the database.
But my website uses lots of Iframe or inner pages which in turn connects to database using a singleton pattern in each page.
I have few questions to be clarified
1)if a user A comes to website, he browses 10 pages, does that for all the pages only one connection is created or for every page one connection is created?
2)For all the DB connections amde in the sub pages will use the same connection or a new one ?
3)When the user exists from the website, how to close the DB Connection?
I am finding tricky to close the DB Connection, Suppose in my index.php, if it has menu.php, stats.php and profile.php, I am not able to make out when to close the connection?
What is the best pratice to close the connection?
Website is build on PHP and MYSQL database.
If you use a singleton, then the connection is established once for every request.
For every of the 10 pages.
If you include your sub pages via iframes, then you're opening a new connection for every sub page.
One option would be to have a single entry point for your webpage (a front controller) and then closing the connection, after the request has been handled. If you include your php pages in your index.php file, then close the connection after the include/require statements.
Some developers believe it is good practice to call for a close of connection at the end of their pages. Some of them also spend time freeing variables and arrays at the end of a script. To me this is a waste of bytes. It's ok to do things just because they are good coding practice but in my opinion, we should be aiming for fast loading web pages and the efficient use of databases in the exchange of data (to and from). The fact is, when you close a php script or navigate to another page, unless both pages are using php sessions then the previous page and all that was on it will die. It no longer exists and php dumps it.
There is no way to retrieve any of that information once this happens as it is truly killed.
As for database connections, they close themselves.
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(). This is stated in the manual under mysql_connect();
Once again, there are those that like to put a line of code at the end of their scripts calling mysql_close($DB) but remember the waste of bytes thing?
If it is already being done, leave it along. Just my 2 cents worth.

PHP Constant open mysql connection safety?

I have a website where at the top of each page I include a page that opens a mysql_connect connection, so I don't have to open up the connection each time I need to modify or insert stuff into the database. Is this safe, or should I open and close each connection once I'm done using it?
require_once("../include/mysqlconn.php");
$sql = "SELECT * FROM blah";
$query = mysql_query($sql);
This is OK as the connection will be closed automatically at the end of each script execution (actually all resources are freed at that point).
Another possibility is to use mysql_pconnect() which does connection caching for you and may speed up your site a bit.
In either case there is nothing to worry about security wise, as long as the password is kept in a place not accessible from outside.
Personally, I create a new connection each time I make a query, then close it. If you have multiple databases, you can clash if you leave it open.
well closing it is safer. you can also include a page that closes the connection to mysql at the end of every page you open the connection.

PHP: closing db connections on included and ajaxed files

When I call to a page through ajax should I close the connection at the end ?
The ajaxed file has a connection string for itself ofcourse, so I wonder if this connection stays open when the call is over, and if it's a disaster to not close these connections...
Thanks in advance to all
When you call a PHP page through AJAX you are actually making a new request to the server to interpret that page. The server does not know anything about the page being requested through AJAX or by typing the address in the browser bar, it just interprets it as a new PHP page.
That said, all pending connections are closed automatically, you can put mysql_close if you wish, otherwise PHP will do it for you.
See the manual page for mysql_close
Using mysql_close() isn't usually
necessary, as non-persistent open
links are automatically closed at the
end of the script's execution.
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.

Categories