Do SQL connections opened with PDO in PHP have to be closed

后端 未结 6 1491
野的像风
野的像风 2020-11-28 07:36

When I open a MySQL connection in PHP with just PHP\'s built-in MySQL functions, I do the following:

$link = mysql_connect($servername, $username, $password)         


        
相关标签:
6条回答
  • 2020-11-28 07:43

    Well seeing as the $link for the PDO is assigned an object, PHP would set that as null as soon as the script runs so that it's no longer an object. Therefore you could just do:

    $link = new PDO("mysql:dbname=$dbname;host=$servername",$username,$password);
    
    //prepare statements, perform queries
    
    $link = null;
    
    0 讨论(0)
  • 2020-11-28 07:51

    http://uk3.php.net/pdo

    From what i gather i could not see anyway to close it in the php manual, and examples of scripts i quickly looked at never closed the connection in anyway from what i could see.

    0 讨论(0)
  • 2020-11-28 07:58

    Use $link = null to let PDO know it can close the connection.

    PHP: PDO Connections & Connection Management

    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.

    0 讨论(0)
  • 2020-11-28 07:59

    PDO does not offer such a function on its own. Connections via PDO are indirectly managed via the PDO objects refcount in PHP.

    But sometimes you want to close the connection anyway, regardless of the refcount. Either because you can not control it, need it for testing purposes or similar.

    You can close the Mysql connection with PDO by running a SQL query. Every user that is able to connect to the Mysql server is able to KILL at least its own thread:

    /*
     * Close Mysql Connection (PDO)
     */
    
    $pdo_mysql_close = function (PDO $connection) {
    
        $query = 'SHOW PROCESSLIST -- ' . uniqid('pdo_mysql_close ', 1);
        $list  = $connection->query($query)->fetchAll(PDO::FETCH_ASSOC);
        foreach ($list as $thread) {
            if ($thread['Info'] === $query) {
                return $connection->query('KILL ' . $thread['Id']);
            }
        }
        return false;
    };
    
    $pdo_mysql_close($conn);
    

    Related Mysql Documentation:

    • 13.7.5.30. SHOW PROCESSLIST Syntax
    • 13.7.6.4. KILL Syntax

    Related Stackoverflow Questions:

    • PHP PDO close()? (Apr 2012)
    0 讨论(0)
  • 2020-11-28 08:02

    When the PHP script finishes executing, all connections are closed. Also you don't have to explicitly close your connection with mysql_close().

    0 讨论(0)
  • 2020-11-28 08:07

    You can also limit your connections to within local functions. That way the connection is closed as soon as the function is completed.

    0 讨论(0)
提交回复
热议问题