I am using PHP to query the MySQL database on my website. Please answer the following questions:
- What will happen if I don’t use
mysql_close()when I am done with querying the database in the end? The connection will remain open? If yes then upto how much time? If no then why? - If I open and close a connection to MySQL at several places in a
webpage, how is the performance affected? i.e. connection is made again everytime some access to database is required on a single webpage. - How is
mysql_close()related to performance? Should I open a new connection everytime some access to database is required OR should I keep only one connection and close it in the end? - If I don’t close the connection, then if the user is trying to
access some data again, will the new connection be used or the old
open connection will be used?
I’d advise to open your database connection during construct phase, re-use that connection during the entire execution of your script (if it’s OO based, assign a class variable for your database connection and use
$this->dbduring the entire script), and close it during destruction (or don’t bother at all closing it, as it will be closed anyway, even when not declared specifically).