lahonda_99 Posted May 26, 2011 Share Posted May 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237526-the-importance-of-mysql_closeconnection/ Share on other sites More sharing options...
fugix Posted May 26, 2011 Share Posted May 26, 2011 non-persistent open links close when the script has ended Quote Link to comment https://forums.phpfreaks.com/topic/237526-the-importance-of-mysql_closeconnection/#findComment-1220589 Share on other sites More sharing options...
Psycho Posted May 26, 2011 Share Posted May 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237526-the-importance-of-mysql_closeconnection/#findComment-1220592 Share on other sites More sharing options...
lahonda_99 Posted May 27, 2011 Author Share Posted May 27, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/237526-the-importance-of-mysql_closeconnection/#findComment-1220921 Share on other sites More sharing options...
PFMaBiSmAd Posted May 27, 2011 Share Posted May 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237526-the-importance-of-mysql_closeconnection/#findComment-1220926 Share on other sites More sharing options...
PFMaBiSmAd Posted May 27, 2011 Share Posted May 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237526-the-importance-of-mysql_closeconnection/#findComment-1220928 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.