为什么要使用 mysqli_close()?
有什么理由让我在查询之后或脚本结束时关闭连接吗?
Is there any reason why I should close the connection after a query or at the end of the script?
做/不做有什么好处?
推荐答案
连接(如果不是持久的) 总是在脚本结束时关闭,所以,理论上,你不会需要自己关闭.
The connection (if not persistent) is always closed at the end of the script, so, in theory, you don't need to close it yourself.
不过,如果您的 PHP 脚本需要大量时间来执行,那么当您不再需要对数据库执行任何请求时关闭连接是个好主意——至少,如果长时间的计算是在之后完成的查询.
Still, if your PHP script takes lots of time to execute, it's a good idea to close the connection when you don't have to do any request to the database anymore -- at least, if the long calculations are done after the queries.
如果您的应用程序部署在共享主机上,则尤其如此:您的用户帐户通常只能同时打开几个连接.(在共享主机上,同时打开的连接数量可能很少;在私人服务器上通常更大).
This is especially true if your application is deployed on a shared hosting : your user account can generally only have a few connections opened at the same time. (That number of simultaneous opened connections can be pretty small on shared hosting ; it's generally bigger on private servers).
我们经常不关闭自己的联系的原因是:
The reason we often don't close connections ourselfves is :
- 我们通常不知道何时完成了所有查询——对于由许多小块"组成的页面尤其如此;每一个都是独立于其他的,并且可以自己进行查询;那么,我们什么时候可以关闭连接?
- 网页的生成速度通常非常快,因此我们不必担心关闭与数据库的连接.
相关文章