0

I'm currently working on a small CMS for my website and I'm getting following error when calling session_start() :

Fatal error: Exception thrown without a stack frame in Unknown on line 0

I'm storing the PDO database connection in the $_SESSION, so I need to call session_start() directly after starting up the script. Here's a snippet :

function initDB($config){ //initalizes the database connection try{ @session_start(); }catch (Exception $e){ } $dsn = 'mysql:dbname='.$config['db'].';host='.$config['host']; $user = $config['usr']; $password = $config['pw']; try { $db = new PDO($dsn, $user, $password); $_SESSION['db'] = $db; } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); } 

Back traced the error to "@session_start()", so I'm not able to suspress the error with @ or even with a try-catch.

I Hope you could help me. Thanks a lot

5
  • Whats the error you are getting? Commented Sep 16, 2010 at 17:26
  • "Fatal error: Exception thrown without a stack frame in Unknown on line 0", as written in the question Commented Sep 16, 2010 at 17:27
  • This doesn't sound like a session error,; this sounds like an exception being raised in an exception handler Commented Sep 16, 2010 at 17:30
  • Storing a resource (i.e. the database connection) will not work, as they're not serializable (the same goes for outside references). Commented Sep 16, 2010 at 17:30
  • Storing a connection? Usually, one just reconnects on the next request... Commented Sep 16, 2010 at 17:41

3 Answers 3

1

You cannot store resources (a PDO object is actually a resource) in a session. On reinitialisation this is broken and throws an exception 'outside' the scope of your PHP file.

Sign up to request clarification or add additional context in comments.

2 Comments

Storing a resource (db connection) in session DOES work, indeed. I can access the object (PDO) from within the $_SESSION-array. I just get the Fatal Error message. If it'd be raised because of the serialization it'd throw an error regarding this fact. But I think I know what you mean. When the page is requested again (same session), the DB connection is re-initialized, so it throws and error, right ? Any idea on storing the database connection in a better way ?
Yes, on re-initialisation is where it goes wrong (actually, on writing it to storage, but you might miss it as the request is 'over'). One doesn't store database connections usually, they're shortlived, and don't survive a second request any more then fopen ed resources do
0

You're probably throwing an exception from a destructor of from an exception handler.


Resources :

On the same topic :

Comments

0

So, as I was told, saving a PDO object in the session does invoke that error. I used a workaround, I'm now setting up da connection for each request, instead of storing connections in the session.

Thanks for your help !

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.