Fatal error: Call to undefined method PDO::close()

*爱你&永不变心* 提交于 2019-12-24 07:34:57

问题


I have got that error and the line was this:

public function __destruct() {
        $this->db->close();
    }

I use PDO, is this the problem, does the PDO driver not recognize this function? If yes, what is its equivalent in PDO?


回答1:


A PDO connection is closed by 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.

so

$this->db = null;

should work.




回答2:


PDO doesn't have an explicit "close" function. You can simply do

$this->db = null

to destroy the object, which will automatically clean up the connections.




回答3:


Setting the PDO variable to null closes the connection and frees all related memory.

An alternative to is to use closeCursor to close the connection with the server but leave the PDO Object around.

"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."

That means you can continue to run fetch() et al, even though the connection has been freed.

Again, not the same as mysql_close(), but nonetheless incredibly useful.




回答4:


You close it by putting the handle to null

From 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;
?>



回答5:


PDO doesn't have a close method. To destroy the connection the PDO object represents, destroy all references to the object itself with unset().




回答6:


PDO hasnt close method. i think you can try to unset $this->db, this disconnect automatically



来源:https://stackoverflow.com/questions/5772626/fatal-error-call-to-undefined-method-pdoclose

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!