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. The constructor receives parameters that specify the database source (so-called DSN) and may also include the user name and password (if any).
Example #1 connected to MySQL
<?php $dbh = new PDO (' Mysql:host=localhost;dbname=test ', $user, $pass);? >
If there are any connection errors, a Pdoexception exception object is thrown. If you want to handle the error state, you can catch an exception, or choose the application global exception handler that is left to be set by Set_exception_handler ().
Example #2 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 (); }? >
If an application does not catch an exception in the PDO constructor, the default action taken by the Zend engine is to end the script and display a backtracking trace that could leak the full database connection details, including the user name and password. It is therefore incumbent to explicitly (through a catch statement) or implicitly (through Set_exception_handler ()) to catch an exception.
Once the connection data is successful, an instance of the PDO class is returned to the script, which remains active for the lifetime of the PDO object. To close the connection, you need to destroy the object to ensure that all references remaining to it are deleted, and you can assign a NULL value to the object variable. If you do not do this explicitly, PHP will automatically close the connection at the end of the script.
Example #3 Close a connection
<?php $dbh = new PDO (' Mysql:host=localhost;dbname=test ', $user, $pass); In this use connection //Now run complete, this closes the connection $DBH = null;? >
Many Web applications benefit from using persistent connections to the database service. Persistent connections are not closed and cached when the script ends, and are reused when another script connects requests that use the same credentials. Persistent connection caching avoids the overhead of creating a new connection each time the script needs to respond to the database, making the Web application faster.
Example #4 Persistent Connection
<?php $dbh = new PDO (' Mysql:host=localhost;dbname=test ', $user, $pass, array ( pdo::attr_persistent = True ));? >
If you want to use persistent connections, you must set pdo::attr_persistent in the drive option array passed to the PDO constructor. If this property is set 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 ODBC connection pooling (there are UNIXODBC and Windows two practices; there may be more), it is recommended that you do not use persistent PDO connections, but instead leave the connection cache to the ODBC connection pool layer processing. ODBC connection pooling is shared with other modules in the process, and if a PDO cache connection is required, this connection is never returned to an ODBC connection pool, resulting in additional connections being created to serve other modules.