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.
Sessions Stored In Db Functions
Started by
bunder5
, Mar 1 2012 3:23 AM
6 replies to this topic
#1
Posted 1 March 2012 - 3:23 AM
#2
Posted 2 March 2012 - 7:18 PM
Um, is there a question here?
#3
Posted 8 March 2012 - 4:00 AM
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.
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.
#4
Posted 8 March 2012 - 2:09 PM
You could check for an error before running both open_session() and write_session().
More info in the PHP Manual.
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.
#5
Posted 20 March 2012 - 12:20 PM
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.
#6
Posted 20 March 2012 - 5:04 PM
Actually $dbc->error_code() is making my pages timeout. I am using isset() to see if that works.
#7
Posted 21 March 2012 - 11:15 AM
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.
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.










