Jump to content
Larry Ullman's Book Forums
bunder5

Sessions Stored In Db Functions

Recommended Posts

I switched over to storing them into a db as done in the book but I notice that when my code has errors, often it leads to an error in the write_session() function. Calling on a function on a non-object, $dbc. The funny thing is that there is no code to write to the session but there is code to read session data.

 

It appears to be trying to write a session before opening the session and establishing a db connection. I confirmed it by putting code inside the write_session() function to open a new connection at each call.

Share this post


Link to post
Share on other sites

I altered your code to use PDO without any problems....except when one of my queries goes bad. Then I get two errors....

 

Fatal error: Call to undefined method PDOStatement::prepare() in \finalize.php on line 139

Fatal error: Call to a member function prepare() on a non-object in \db_sessions.inc.php on line 84

 

The first was from a failed query. The second is line #84 which is the write_session() function. The funny thing is that if I insert code to establish $dbc inside write_session() and rerun that would eliminate the second error.

 

Then I put simple echo statements in the open_session() and write_session() and whenever my query goes bad it fails to open_session() before write_session() which is why I get the second error. Do you know why this is happening?

 

 

Sorry for late response, I did not subscribe to topic.

Share this post


Link to post
Share on other sites

You could check for an error before running both open_session() and write_session().

if ( $dbc->errorCode() == '01' ) {
   // no error, proceed to open_session and write_session
} else {
   // an error happened, use errorInfo() to get details
}

 

More info in the PHP Manual.

  • Upvote 1

Share this post


Link to post
Share on other sites

That works but I don't get why sometimes it would be attempting to write to session before opening the session. I trapped it and it goes to write first and I have not done anything with sessions.

Share this post


Link to post
Share on other sites

I'm making the assumption that your database resource link is stored in $dbc. If you use a different variable name, you'll need to change that conditional. Be sure to check the PHP Manual page on the proper use of error_code(). The example block of code should only attempt to process your PDO statements if there were no errors in your last database operation.

 

I can't provide too much help here, since I've never used PDO myself. In fact, we have such an old version of PHP on our server, I've stuck to procedural coding, so I'm not very well versed in OOP.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×