Jump to content

The Importance of mysql_close($connection);


lahonda_99

Recommended Posts

Hello members. I am wondering if "mysql_close($connection);" needs to be put at the end of every .php page that connects to my MySQL database or if I can just place it at the end of the logout script since that will be the last page any user accesses. Is it important to close the connection between PHP and MySQL after each page has been generated?

 

I ask because I have been getting very embarrassed in front of my boss because on work computers the pages I code have multiple timeouts when connecting to the DB. I am hoping that there is a way to connect to the DB and leave the connection open for a while so the user experiences less timeouts. I've messed with the timeout variable in php.ini, but its default was 3 seconds and I kept it at 5. The timeout returns an error plenty before 3 seconds. My DB host is a reliable remote DB; servage.net, so it can't be their problem.

Link to comment
Share on other sites

I ask because I have been getting very embarrassed in front of my boss because on work computers the pages I code have multiple timeouts when connecting to the DB.

 

Do you have queries running within loops? That is the absolutely worst thing you could be doing with regard to efficient programming in reference t DB activities.

 

Post the script from one of the pages that is timing out and we may be able to help you fix the problems.

Link to comment
Share on other sites

Okay, so I don't need to explicitly close the database connection because it will close at the end of the PHP script. Fugix, would making my link to the DB persistent help to cut down on time outs?

 

Damato, thanks for offering to look through my code. The body of my PHP can be found here. I checked and I do not have any queries running within loops. I agree that doing so would probably take a lot of server side time.

 

The first part of my script checks to see if a $_POST is set with the value 'new.' Meaning that the user created a new entry from the form at the bottom of the script.

 

The second part displays the current records. The third part is the form for users to create new entries.

 

I've gotten time outs from accessing the page sometimes, but mostly after trying to create new records. Meaning that $_POST['new'] was set.

 

I greatly appreciate that any who respond do so out of their free time. Thank you for helping me :)

Link to comment
Share on other sites

A persistent database connection is released (it goes back into the pool of available persistent connections) by the script when the execution on a page ends, assuming that your web server/php combination supports persistent connections in the first place.

 

The ONLY difference between non-persistent and persistent connections is that if there is an available persistent connection, you save the overhead needed to make the actual connection to the database server. You must still call the code necessary to get an existing connection/form a new connection if there are no available connections.

 

The only way a persistent connection would help with a time-out situation would be if making the initial connection took an abnormally long time. There are literally millions of php/mysql based web pages that don't use and don't even support persistent connections that work as expected.

Link to comment
Share on other sites

My DB host is a reliable remote DB

 

You do realize that making a connection to a remote database server -

 

A) Has more overhead to just establish the connection using an Internet protocol.

 

B) Is subject to the reliability of the connection between where you are at and where the database server is at.

 

C) Has a transfer rate that is several times slower than a local database server, which would immediately impact any web page that executes several queries and/or retrieves a lot of data.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.