Php -- PDO-connection and connection management

Source: Internet
Author: User
The PDO of the PHP database abstraction layer-connections and connection management connections are established by creating a PDO base class instance. No matter which driver is used, the PDO class name is used. The constructor receives parameters used to specify the database source (the so-called DSN) and possibly including the user name and password (if any.

Example #1 connect to MySQL

 

If any connection error occurs, a PDOException object is thrown. If you want to handle the error status, you can capture the exception or leave it to the global exception handling program of the application set through set_exception_handler.

Example #2 handle connection errors

 query('SELECT * from FOO') as $row) {            print_r($row);        }        $dbh = null;    } catch (PDOException $e) {        print "Error!: " . $e->getMessage() . "
"; die(); }?>

If the application does not capture exceptions in the PDO constructor, the default action taken by the zend Engine is to end the script and display a tracing trace. This tracing trace may leak complete database connection details, including the user name and password. Therefore, it is the responsibility to catch exceptions explicitly (via catch statements) or implicitly (via set_exception_handler.

After the data is successfully connected, a PDO class instance is returned to the script. this connection remains active during the lifecycle of the PDO object. To close the connection, you need to destroy the object to ensure that all references to the remaining object are deleted. you can assign a NULL value to the object variable. If you do not explicitly do this, PHP will automatically close the connection when the script ends.

Example #3 close a connection

 

Many web applications benefit from using persistent connections to database services. Persistent connections are not closed and cached after the script ends. they are reused when another script with the same creden。 connects to the request. Persistent connection cache can avoid the overhead of creating a new connection between each script and the database, so that the web application can be faster.

Example #4 persistent connection

  true    ));?>

To use persistent connections, you must set PDO: ATTR_PERSISTENT in the driver option array passed to the PDO constructor. If you set this property with PDO: setAttribute () after object initialization, the driver will not use persistent connections.

If you use the pdo odbc driver and the ODBC Library supports the ODBC Connection pool (either unixODBC or Windows; there may be more), we recommend that you do not use persistent PDO connections, instead, the connection cache is left to the ODBC Connection pool layer for processing. The ODBC Connection pool is shared with other modules in the process. if the PDO cache connection is required, the connection will never be returned to the ODBC Connection pool, resulting in the creation of additional connections to serve other modules.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.