Storing database connection in a session variable [duplicate]

You can’t store database connections or result sets in the session, since those are resources, and:

Some types of data can not be serialized thus stored in sessions. It includes resource variables or objects with circular references (i.e. objects which passes a reference to itself to another object).

http://php.net/manual/en/intro.session.php

You can extract a result set into a normal array and store that in the session like any other variable. That would be a fairly typical use case for sessions anyway. Just be careful not to store too much data in the session, as that can become more taxing than fetching it from the database.

Leave a Comment