Jump to content


when is the right time to close a MySQL database?

  • Please log in to reply
5 replies to this topic

#1 bilis_money

  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 11 July 2006 - 12:07 PM

Ok, i know how to open and close a MySQL database with PHP.

But i'm really confuse when is the right time to close it after opening?
PLease if you have a bright answer and detailed and smart explaination for
this please don't hesitate to share it to me, because i want to know it.

Thank you very much.

#2 ShogunWarrior

  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 11 July 2006 - 12:10 PM

Generally, you will close it when you know it will no longer be need so, you might do on a page:

$conn = mysql_connect(.....);

$conn = mysql_query($sql....);
//Use the database again
$x = getArticles($conn);

//Now we'll output html, no need for mysql


<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#3 Kris

  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 11 July 2006 - 12:26 PM

You actually don't even need to close it most of the time as the link to the server will be closed as soon as the execution of the script ends. You only need to close it with mysql_close() if you have a specific reason to do so.

#4 hvle

  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 11 July 2006 - 12:28 PM

I don't close my connection.  The script close automaticly upon exit anyway.
And I used pconnect (persistent connection) instead of connect.

Personally, I don't see any security issue with that.
Life's too short for arguing.

#5 bilis_money

  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 11 July 2006 - 01:05 PM

nice!, thanks!

#6 wildteen88

  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 11 July 2006 - 04:49 PM

You'll only want to use mysql_close when using mysql_pconnect. As persistant connections will not close themselves, hens perisistant connections. However non-persistant connects wont need thier connection closed manually as they exit automatically once the script has been executed.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users