問題描述
與 MySQLi 相比,PDO 只是一個相當簡單的問題.
Just a rather simple question with regards to PDO compared to MySQLi.
使用 MySQLi,您可以關閉連接:
With MySQLi, to close the connection you could do:
$this->connection->close();
但是對于 PDO,它聲明您使用以下方式打開連接:
However with PDO it states you open the connection using:
$this->connection = new PDO();
但要關閉連接,請將其設置為 null
.
but to close the connection you set it to null
.
$this->connection = null;
這是否正確,這是否會真正釋放 PDO 連接?(我知道它設置為 null
.)我的意思是使用 MySQLi 你必須調用一個函數 (close
) 來關閉連接.PDO 是否像 = null
一樣容易斷開連接?或者有沒有關閉連接的功能?
Is this correct and will this actually free the PDO connection? (I know it does as it is set to null
.) I mean with MySQLi you have to call a function (close
) to close the connection. Is PDO as easy as = null
to disconnect? Or is there a function to close the connection?
推薦答案
根據文檔,您是正確的 (http://php.net/manual/en/pdo.connections.php):
According to documentation you're correct (http://php.net/manual/en/pdo.connections.php):
連接在該 PDO 對象的生命周期內保持活動.到關閉連接,您需要通過確保銷毀對象對它的所有剩余引用都將被刪除——您可以通過分配NULL 到保存對象的變量.如果你不這樣做明確地,PHP 會在您輸入時自動關閉連接腳本結束.
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.
請注意,如果您將 PDO 對象初始化為持久連接,它不會自動關閉連接.
Note that if you initialise the PDO object as a persistent connection it will not automatically close the connection.
這篇關于PDO 關閉連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!