Page 1 of 1

Session Variable Question

Posted: Sun Jul 04, 2010 8:50 pm
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?

Re: Session Variable Question

Posted: Sun Jul 04, 2010 9:13 pm
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.

Re: Session Variable Question

Posted: Mon Jul 12, 2010 1:23 pm
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.