Last Updated: June 11, 2021
·
9.174K
· shaybenmoshe

PDO Persistent Connection

PDO lets us open the connection as a persistent connection.
This means that rather than establishing a new connection with each request, the connection to the database is cached and reused.

Persistent connection consumes a lot less memory and CPU time.

Establishing a persistent connection is simple, just pass the relevant parameter in the initialization.
For example, instead of

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

use

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

A more complete analysis can be found here: http://blog.shay.co/pdo-persistent-connection-analysis/

3 Responses
Add your response

Note that this doesn't wroks if php run as CGI,
Persistent connections are only supported if you’re using PHP in the mod_php variety, and not the CGI wrapper.

over 1 year ago ·

@samuelm Thank you, didn't know that.

over 1 year ago ·

Apart from persistent connection, you can also easily perform CRUD operations in PDO. You can use PDO connection in PHP easily by using its connection class.

over 1 year ago ·