How to close php pdo connection

藏色散人
Release: 2023-03-10 15:56:02
Original
1611 people have browsed it

php pdo connection closing method: first create a PHP sample file; then connect to MySQL; finally close the connection through the "$dbh = null;" method.

How to close php pdo connection

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to close the php pdo connection?

PHP PDO Connection

The connection is established by creating an instance of the PDO base class. Regardless of which driver is used, the PDO class name is used.

Connecting to MySQL

<?php
$dbh = new PDO(&#39;mysql:host=localhost;dbname=test&#39;, $user, $pass);
?>
Copy after login

Note: If there are any connection errors, a PDOException exception object will be thrown.

Handling connection errors

<?php
try {
    $dbh = new PDO(&#39;mysql:host=localhost;dbname=test&#39;, $user, $pass);
    foreach($dbh->query(&#39;SELECT * from FOO&#39;) as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>
Copy after login

After successfully connecting the data, an instance of the PDO class is returned to the script. This connection remains active during the life cycle of the PDO object.

To close the connection, you need to destroy the object to ensure that all remaining references to it are deleted. You can assign a NULL value to the object variable.

If you don't do this, PHP will automatically close the connection at the end of the script.

Close a connection:

<?php
$dbh = new PDO(&#39;mysql:host=localhost;dbname=test&#39;, $user, $pass);
// 在此使用连接
// 现在运行完成,在此关闭连接
$dbh = null;
?>
Copy after login

Many web applications benefit from using persistent connections to database services.

Persistent connections are not closed after the script ends, and are cached and reused when another script connection request using the same credentials is made.

Persistent connection caching can make web applications faster by avoiding the overhead of establishing a new connection every time a script needs to talk back to the database.

Persistent connections

<?php
$dbh = new PDO(&#39;mysql:host=localhost;dbname=test&#39;, $user, $pass, array(
    PDO::ATTR_PERSISTENT => true
));
?>
Copy after login

Note: If you want to use persistent connections, you must set PDO::ATTR_PERSISTENT in the driver options array passed to the PDO constructor. If this attribute is set with PDO::setAttribute() after the object has been initialized, the driver will not use persistent connections.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to close php pdo connection. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!