I'm using PHP to question the MySQL database on my small website. Please answer the next questions:
- What's going to happen basically avoid using
mysql_close()after i am completed with querying the database ultimately? The bond will stay open? If so then up to the length of time? If no then why?
- Basically open and shut an association to MySQL at a number of places inside a web page, how's the performance affected? i.e. connection is created again each time some use of database is needed on one web page.
mysql_close()associated with performance? Must I open a brand new connection each time some use of database is needed OR must i keep just one connection and close it ultimately?
- Basically don't close the bond, then when the user is attempting to access some data again, will the brand new connection be utilized or even the old open connection is going to be used?
- It'll instantly close once the PHP script is performed running throughout destruct phase.
- Performance will adversely suffer. Opening a brand new socket (especially for an exterior database server) is much more costly and time intensive than simply keeping a pointer to the present connection in memory.
- See answer no. 2
- The accessibility data is going to be carried out with a new PHP request. Hence, you'll have a new database connection. No problems there.
I'd advise to spread out your database connection throughout construct phase, re-use that connection throughout the whole execution of the script (whether it's OO based, assign a category variable for the database connection and employ
$this->db throughout the whole script), and close it throughout destruction (or think before whatsoever closing it, as it will likely be closed anyway, even if not declared particularly).
If you are using non-persistent connections all opened up MySQL connections is going to be instantly closed once the PHP script finishes execution.
From php.internet :
Using mysql_close() is not usually necessary, as non-persistent open links are instantly closed in the finish from the script's execution.
for performance it is dependent on situations, how lengthy it's used, based on how lengthy it's idle and so forth (e.g. lengthy execution). Generally, there's singleton pattern by that you've one open connection, making all queries with this open handle. But it is not the case overall, as mysql_connect is type of supports that:
If your second call is built to mysql_connect() with similar arguments, no new link is going to be established, but rather, the hyperlink identifier from the already opened up link is going to be came back. The brand new_link parameter modifies this behavior and makes mysql_connect() always open a brand new link, even when mysql_connect() was known as before with similar parameters.
So essentially, mysql_close is not so needed if this involves short running scripts.
There's minimal performance loss to shut an association, in comparison towards the resource use of keeping it open when you do not need it. Easier to close an association the moment you are completed with it, and open it up again when you really need to.