php method to close pdo: first connect to MySQL through PHP; then return an instance of the PDO class to the script; finally close the connection by assigning a NULL value to the object variable.
Recommended: "PHP Video Tutorial"
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('mysql:host=localhost;dbname=test', $user, $pass); ?>
Note: If there are any connection errors, a PDOException exception object will be thrown.
Handling connection errors
<?php try { $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); foreach($dbh->query('SELECT * from FOO') as $row) { print_r($row); } $dbh = null; } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); } ?>
After successfully connecting 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('mysql:host=localhost;dbname=test', $user, $pass); // 在此使用连接 // 现在运行完成,在此关闭连接 $dbh = null; ?>
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('mysql:host=localhost;dbname=test', $user, $pass, array( PDO::ATTR_PERSISTENT => true )); ?>
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.
The above is the detailed content of How to close pdo in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
