Home  >  Article  >  Backend Development  >  php--PDO-Connection and connection management

php--PDO-Connection and connection management

伊谢尔伦
伊谢尔伦Original
2016-11-22 09:36:531289browse

Connections are established by creating an instance of the PDO base class. Regardless of which driver is used, the PDO class name is used. The constructor receives parameters specifying the database source (so-called DSN) and possibly the username and password (if any).

Example #1 Connecting to MySQL

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

If there are any connection errors, a PDOException exception object will be thrown. If you want to handle error conditions, you can catch the exception, or choose to leave it to the application global exception handler set via set_exception_handler() .

Example #2 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();
    }
?>

If the application does not catch the exception in the PDO constructor, the default action taken by the zend engine is to end the script and display a backtrace, this backtrace may leak the complete database connection details, including the user name and password. It is therefore the responsibility to catch exceptions either explicitly (via catch statement) or implicitly (via set_exception_handler()).

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 explicitly, PHP will automatically close the connection at the end of the script.

Example #3 Closing a connection

<?php
    $dbh = new PDO(&#39;mysql:host=localhost;dbname=test&#39;, $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 to the database.

Example #4 Persistent Connection

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

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.

If you use the PDO ODBC driver and the ODBC library supports ODBC connection pooling (there are two approaches, unixODBC and Windows; there may be more), it is recommended not to use persistent PDO connections, but leave the connection cache to the ODBC connection pool layer. . The ODBC connection pool is shared with other modules in the process; if PDO is asked to cache a connection, the connection is never returned to the ODBC connection pool, causing additional connections to be created to serve other modules.


Statement:
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