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).

Example #1 Connecting to MySQL <?php

$dbh = 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().

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();

}

?>

Warning 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.

Note: If there are still other references to this PDO instance (such as from a PDOStatement instance, or from other variables referencing the same PDO instance), these have to be removed also (for instance, by assigning NULL to the variable that references the PDOStatement).

Example #3 Closing a connection <?php

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

// use the connection here

$sth = $dbh -> query ( 'SELECT * FROM foo' );



// and now we're done; close it

$sth = null ;

$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.

Example #4 Persistent connections <?php

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

PDO :: ATTR_PERSISTENT => true

));

?>

The value of the PDO::ATTR_PERSISTENT option is converted to boolean (enable/disable persistent connections), unless it is a non-numeric string, in which case it allows to use multiple persistent connection pools. This is useful if different connections use incompatible settings, for instance, different values of PDO::MYSQL_ATTR_USE_BUFFERED_QUERY .

Note: 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.