Saving a database connection to a session variable

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
tfneves
Forum Newbie
Posts: 2
Joined: Tue Sep 10, 2002 8:03 am

Saving a database connection to a session variable

Post by tfneves »

Hi All!

I have a problem. I can save strings, integers to session variables and retrieve them from page to page. But when it comes to a database connection I cannot save it. Does anybody have a solution to this?

Also, what is the most secure method for using session vars?

Thanks,
Tony
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

How do you mean by storing database connections... Do you mean like this?

Code: Select all

<?php
$connection = mysql_connect("locahost","user","password");
$_SESSIONї'connection'] = $connection;
?>
If you're doing this it's not going to work because $connection hasn't got a connection it's just got the results of connection. If you want to keep the connection open use mysql_pconnet();
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you cannot save ressources to sessions.
take a look at The magic functions __sleep and __wakeup as example as how to restore ressources like db-connections
Post Reply