Current location: Hot Scripts Forums » Programming Languages » PHP » Closing a MySQL connection

Closing a MySQL connection

Reply
  #1 (permalink)  
Old 08-13-03, 04:54 AM
surebetdmg surebetdmg is offline
Newbie Coder
 
Join Date: Aug 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Closing a MySQL connection

Hi there.

Would I be right in believing that one should close a MySQL connection after one has finished using it? eg if your page adds members to database, at the beginning of the page you would open the connection, then you would carry out the relevent quieries, then, finally, close the connection?

If so, is the following possible?

Store all the database info (username, password etc) in db.inc.php. Also put in this file the command to open the DB connection.

In the file footer.inc.php (which would be added to all pages on the site) have a test to see if a DB connection is open, and if so, close it.

Is this possible? Can one test for an open connection? (if so, how? )

Secondly, is this the most efficient way of doing this. The site I have in mind will be making very frequent use of the database; is it possible (and advisable) to keep the DB connection open?

Sorry if this sounds complicated, or just non-sensical. Thanks in advance for your help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-13-03, 09:04 AM
e4c5 e4c5 is offline
Newbie Coder
 
Join Date: Aug 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

Though it's always a good idea to close open file handles, it's not always a good idea to close database connections.

If you are working with PHP there isn't a real need to explicity close mysql connections because they can be reused between pages.
__________________
Raditha Dissanayake

<a href="http://www.radinks.com/sftp">http://www.radinks.com/sftp</a>
Lean and mean sftp applet with GUI.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 08-13-03, 01:38 PM
surebetdmg surebetdmg is offline
Newbie Coder
 
Join Date: Aug 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
I read on a tutorial (I think on phpfreaks.com; not sure) that it was bad to leave DB connections open. I can't remember their reason for this; it might have been something to do with the webhost having to waste bandwidth or something??? not sure

So what you are saying is that I can open a connection at the beginning of a php script, and not have to bother to close it? I hope you are right, since it would make my life a hell of a lot easier. Does anyone else agree/disagree?

Thanks for your help.

Archie
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 08-14-03, 08:59 AM
e4c5 e4c5 is offline
Newbie Coder
 
Join Date: Aug 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

The reason that hosting companies encourage you to close connections is that only a limited number of sockets connections can be opened on any server (be it windows or linux)

On a shared server this can be used up pretty quickly.

The connection opening in fact has some overheads. So on well managed connection pooling systems (not talking just of PHP here) when you attempt to close the connection what really happens is that the connection is returned to a pool. It's kept there for a few minutes in the hope that someone will claim it. After that it's closed automatically.

well this is atleat my understanding of matters :-))
__________________
Raditha Dissanayake

<a href="http://www.radinks.com/sftp">http://www.radinks.com/sftp</a>
Lean and mean sftp applet with GUI.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 08-14-03, 06:37 PM
surebetdmg surebetdmg is offline
Newbie Coder
 
Join Date: Aug 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for your help.

Just found this in the php manual:

Quote:
Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 08-15-03, 08:37 AM
e4c5 e4c5 is offline
Newbie Coder
 
Join Date: Aug 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
there you are
have fun.
__________________
Raditha Dissanayake

<a href="http://www.radinks.com/sftp">http://www.radinks.com/sftp</a>
Lean and mean sftp applet with GUI.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
Forum/Board - MySQL = Possible? cipher PHP 7 10-07-03 03:42 AM
great product for dumping/recovering MySQL databases Dave Brown General Advertisements 1 10-03-03 07:40 AM
mysql to access aspuser25 Database 2 09-16-03 11:01 AM
mysql v2UK ASP 2 08-18-03 08:50 PM


All times are GMT -5. The time now is 06:41 AM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.