О компании
Разработка сайтов
Продвижение сайтов
Исследования и аудит
Наши заказчики
+7 (812) 710-32-88
дополнительная информация:

ICQ консультация268-126-780
Вам обязательно ответят!

Connections and Connection management

Connections are established by creating instances of the PDO base class. It doesn't matter which driver you want to use; you always use the PDO class name. The constructor accepts parameters for specifying the database source (known as the DSN) and optionally for the username and password (if any).

Пример #1 Connecting to MySQL

= new PDO('mysql:host=localhost;dbname=test'$user$pass);

If there are any connection errors, a PDOException object will be thrown. You may catch the exception if you want to handle the error condition, or you may opt to leave it for an application global exception handler that you set up via set_exception_handler().

Пример #2 Handling connection errors

try {
$dbh = new PDO('mysql:host=localhost;dbname=test'$user$pass);
$dbh->query('SELECT * from FOO') as $row) {
$dbh null;
} catch (
PDOException $e) {
"Error!: " $e->getMessage() . "<br/>";


If your application does not catch the exception thrown from the PDO constructor, the default action taken by the zend engine is to terminate the script and display a back trace. This back trace will likely reveal the full database connection details, including the username and password. It is your responsibility to catch this exception, either explicitly (via a catch statement) or implicitly via set_exception_handler().

Upon successful connection to the database, an instance of the PDO class is returned to your script. The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.

Пример #3 Closing a connection

= new PDO('mysql:host=localhost;dbname=test'$user$pass);
// use the connection here

// and now we're done; close it
$dbh null;

Many web applications will benefit from making persistent connections to database servers. Persistent connections are not closed at the end of the script, but are cached and re-used when another script requests a connection using the same credentials. The persistent connection cache allows you to avoid the overhead of establishing a new connection every time a script needs to talk to a database, resulting in a faster web application.

Пример #4 Persistent connections

= new PDO('mysql:host=localhost;dbname=test'$user$pass, array(

Замечание: If you wish to use persistent connections, you must set PDO::ATTR_PERSISTENT in the array of driver options passed to the PDO constructor. If setting this attribute with PDO::setAttribute() after instantiation of the object, the driver will not use persistent connections.

Замечание: If you're using the PDO ODBC driver and your ODBC libraries support ODBC Connection Pooling (unixODBC and Windows are two that do; there may be more), then it's recommended that you don't use persistent PDO connections, and instead leave the connection caching to the ODBC Connection Pooling layer. The ODBC Connection Pool is shared with other modules in the process; if PDO is told to cache the connection, then that connection would never be returned to the ODBC connection pool, resulting in additional connections being created to service those other modules.

Copyright (C) 2003 Urlixтел: (812) 710-32-88 mailto: contact@urlix.ru

Наш офис находится по адресу: Санкт-Петербург, ул. Разъезжая, д.20, офис 41

Rambler's Top100