SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    closing database connections

    Not sure if this is a php or mysql question but since it originates in php, I'll ask it here. I have a database connection file and I use an include throughout the program in different places to establish a db connection. My question regards how those connections close and how long they remain open and what is the consequence of that if any?

    First I always use include rather than include_once, does that matter.

    And secondly, I never close them. Is there a way to do that and should I be doing that?

    Thanks

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The connections one established remain till end of the script (included and direct) if it is not closed manually by mysql_close() function. So once you establish the connection you don't have to connect again if you haven't closed it. Since the manual itself says
    Opens or reuses a connection to a MySQL server.
    It reuses the connection if it is already connected to the server. I hope you can find the use (differences) of include_once and include functions. There is no any bad consequence by unclosed connections as far as I know.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,817
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Unless you expect your script to run for more than 1/10th of a second or so (in other words - a really long time) then you don't really need to worry about closing the connection as it will close automatically when the script ends.

    It is only where you have long running scripts where most of the processing occurs after you have finished with the database that you would benefit from closing it.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bostboy View Post
    Not sure if this is a php or mysql question but since it originates in php, I'll ask it here. I have a database connection file and I use an include throughout the program in different places to establish a db connection. My question regards how those connections close and how long they remain open and what is the consequence of that if any?

    First I always use include rather than include_once, does that matter.

    And secondly, I never close them. Is there a way to do that and should I be doing that?

    Thanks
    imho it's good practice and "house-keeping" to close db connections and free results sets when you know they will no longer be needed in a script.

    Quote Originally Posted by bostboy View Post
    First I always use include rather than include_once, does that matter.
    Read This For More


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •