You should not store PDOs in sessions.
The best (and only right) way to use PDOs is to create them for every request to the server.
The advantage of prepared queries is 2 ways:
- When executing the same request several times there is an advantage in speed
- It is possible to bind parameters to prevent SQL injection.
When storing a PDO resource in a session, an open connection to the database will be created, as requests from different clients arrive. PDO does a connection pool, trying to minimize database connections, but still with some connections open to speed. By keeping pdo connections in the session, this mechanism is killed. And performance will suffer.
Jvdberg
source share