2011-04-24 162 views

回答

8

一個PDO連接是通過destroying its object:

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.

關閉,以便

$this->db = null; 

應該工作。

+0

thx很多朋友:) – Malloc 2011-04-24 19:15:30

0

PDO沒有密切的方法。要銷燬PDO對象表示的連接,使用unset()銷燬對該對象本身的所有引用。

1

PDO沒有明確的「關閉」功能。你可以簡單地做:

$this->db = null 

銷燬對象,它會自動清理連接。

1

您可以通過把手柄null

從php.net關閉:

<?php 
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); 
// use the connection here 

// and now we're done; close it 
$dbh = null; 
?> 
0

PDO處理不當close方法。我認爲你可以嘗試取消設置$ this-> db,自動斷開連接

2

將PDO變量設置爲null會關閉連接並釋放所有相關的內存。

另一種方法是使用closeCursor關閉與服務器的連接,但保留PDO對象。

" closeCursor frees up the connection to the server so that other SQL statements may be issued, but leaves the statement in a state that enables it to be executed again."

這意味着即使連接已被釋放,您仍然可以繼續運行fetch()等。

再次,不同於mysql_close(),但仍然非常有用。

相關問題