Jump to content

Archived

This topic is now archived and is closed to further replies.

bilis_money

when is the right time to close a MySQL database?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Generally, you will close it when you know it will no longer be need so, you might do on a page:

[code]
<?php
$conn = mysql_connect(.....);

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

//Now we'll output html, no need for mysql
mysql_close($conn);

renderHtml($x....);
?>[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.