Session Variable Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jcobban
Forum Commoner
Posts: 41
Joined: Mon Mar 08, 2010 7:40 am

Session Variable Question

Post by jcobban »

I am trying to save an MDB2 prepared statement so that I can reuse it for subsequent requests during a session. But I get the error:

Fatal error: logSqlUpdate(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "MDB2_Statement_mysql" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in /home/jcobban/includes/legacy.inc on line 619

Code: Select all

function logSqlUpdate($command, $connection)
{
    global $debug;

    // prepare the insert
    if (!isset($_SESSION['logCmdPrep']))
    {
	$logCmdText	= "INSERT INTO SqlLog (SL_DateTime, SL_Command)
			VALUES (NOW(), ?);";
	$_SESSION['logCmdPrep']	= $connection->prepare($logCmdText);
    }
    
    // perform the update
    $result	= $_SESSION['logCmdPrep']->execute($command);
    if (PEAR::isError($result)) {
	die($result->getMessage());
    }
    if ($debug)
	print "<p>logged: $command</p>\n";
    
}		// logSqlUpdate
What am I doing wrong?
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Session Variable Question

Post by jraede »

It looks like the $connection->prepare() method returns an object. When you save an object as a session variable, it gets serialized, and when you retrieve it, it gets unserialized. The serialization stores the class of the object, and when it unserializes it, it reinstantiates the object with that saved class. You're getting that error because you aren't including the file with the class definition for whatever type of object $connection->prepare() returns, or you may be including it, but after you try to run the execute() method on the object recalled from the session variable.
jcobban
Forum Commoner
Posts: 41
Joined: Mon Mar 08, 2010 7:40 am

Re: Session Variable Question

Post by jcobban »

jraede wrote:It looks like the $connection->prepare() method returns an object. When you save an object as a session variable, it gets serialized, and when you retrieve it, it gets unserialized. The serialization stores the class of the object, and when it unserializes it, it reinstantiates the object with that saved class. You're getting that error because you aren't including the file with the class definition for whatever type of object $connection->prepare() returns, or you may be including it, but after you try to run the execute() method on the object recalled from the session variable.
Thank you. To get around this, I just had to store the reference to the prepared statement instead of the prepared statement itself.
Post Reply