問題描述
有什么理由讓我在查詢之后或腳本結(jié)束時關(guān)閉連接嗎?
Is there any reason why I should close the connection after a query or at the end of the script?
做/不做有什么好處?
推薦答案
連接(如果不是持久的) 總是在腳本結(jié)束時關(guān)閉,所以,理論上,你不會需要自己關(guān)閉.
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 腳本需要大量時間來執(zhí)行,那么當(dāng)您不再需要對數(shù)據(jù)庫執(zhí)行任何請求時關(guān)閉連接是個好主意——至少,如果長時間的計(jì)算是在之后完成的查詢.
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.
如果您的應(yīng)用程序部署在共享主機(jī)上,則尤其如此:您的用戶帳戶通常只能同時打開幾個連接.(在共享主機(jī)上,同時打開的連接數(shù)量可能很少;在私人服務(wù)器上通常更大).
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).
我們經(jīng)常不關(guān)閉自己的聯(lián)系的原因是:
The reason we often don't close connections ourselfves is :
- 我們通常不知道何時完成了所有查詢——對于由許多小塊"組成的頁面尤其如此;每一個都是獨(dú)立于其他的,并且可以自己進(jìn)行查詢;那么,我們什么時候可以關(guān)閉連接?
- 網(wǎng)頁的生成速度通常非???,因此我們不必?fù)?dān)心關(guān)閉與數(shù)據(jù)庫的連接.
這篇關(guān)于為什么要使用 mysqli_close()?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!