問題描述
我注意到 PDO 沒有關閉功能.我應該關閉連接還是 PDO 不需要?
成功連接到數據庫后,PDO 類的實例將返回到您的腳本.該連接在該 PDO 對象的生命周期內保持活動狀態.要關閉連接,您需要通過確保刪除對它的所有剩余引用來銷毀該對象——您可以通過將 NULL 分配給保存該對象的變量來完成此操作.如果您沒有明確這樣做,PHP 將在您的腳本結束時自動關閉連接.
http://php.net/manual/en/pdo.connections.php>
所以答案是否定的,除非出于某種原因需要在腳本執行期間顯式關閉連接,否則您不需要做任何事情,在這種情況下,只需將 PDO 對象設置為 null.
I noticed there is no close function for PDO. Should I close the connection or is it unnecessary for PDO?
Upon successful connection to the database, an instance of the PDO class is returned to your script. The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.
http://php.net/manual/en/pdo.connections.php
So the answer is no, you don't need to do anything unless you need to explicitly close the connection during the script execution for whatever reason, in which case just set your PDO object to null.
這篇關于是否有必要關閉 PDO 連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!