I am using PHP to query the MySQL database on my website. Please answer the following questions:
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?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 you open the connection and don't close it, then it would decrease the connection pools and limits available for connecting to database again. It is always recommended to close the connection and data reader objects explicitly when you use them. Please Sign up or sign in to vote.
If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters.
This means that it does not represent current open connections, but all connection attempts since the server has been started. The variable showing number of current open connections, again according to MySQ docs, is Threads_connected.
According to MySQL documentation the Connections status variable shows "The number of connection attempts (successful or not) to the MySQL server." This means that it does not represent current open connections, but all connection attempts since the server has been started.
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->db
during 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With