Hello I serialized an object (Class Users) to set it in $_SESSION array,when the user login my website that object is set it in $_SESSION and later is unserialized for use methods,but I have a problem,on that class I pass the PDO Object in __constructor(that is connected to MySQL) and I set a member variable to PDO Object, but when I unserialize the "container object" it doesn't execute __constructor function,so the member variable that is a PDO object will not be set.. I tried to serialize the PDO Object too,but I can't serialize PDO Object adding a __sleep() and __wakeup() functions in a extended "MyPDO" Class How can I solve it? Thanks
0
votes
Please re construct your question with some code
- diEcho
Doesn't change the question, but you don't have to serialize your array manually to set it in $_SESSION, it is done automatically.
- theredled
Why don't you use __wakeup() to set your PDO variable ?
- theredled
Thanks for the reply,what I want to do is store a user in session (with methods and member variables like iduser,name,lastnames..)but that needs a connection to database,but each user only create one sql connection because there is only one PDO Object,this is the summary of my code: class Users(){ function __construct(MyPDO $mypdo){ $this->MyPDO = $mypdo; } function __sleep(){ return array("iduser","name","lastnames"); } function __wakeup(){ error_log("waking up.."); } } class MyPDO extends PDO{ }
- Carlos
2 Answers
0
votes
First of all you should not store pdo object in session as there is a limit to total open connection which will result you in an error 'too many connections' when all connection will be accrued. it means there is mysql configuration setting my.cnf for max_connection which defines maximum concurrent connection can be made to mysql database. so you should not save pdo in session. recommendation states even connection should be created after creating sql query string.
For every request you should create new connection. as follow
// create sql query
// create connection to database
// begin transaction if require
// prepare statement
// execute query
// close statement object
// // commit changes if transaction