p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

php_databases thread: Problem storing DB obj in session


Message #1 by Vinoaj Vijeyakumaar <vinoaj@s...> on Mon, 13 Jan 2003 01:41:05 +1100
Hi,

To see a working mysql class in php go here..

http://www.hotscripts.com/Detailed/18290.html

Cheers,
Justin.


> -----Original Message-----
> From: Vinoaj Vijeyakumaar [mailto:vinoaj@s...]
> Sent: 12 January 2003 14:41
> To: php_databases
> Subject: [php_databases] Problem storing DB obj in session
> 
> 
> Hi,
> 
> The first time I started programming in PHP was about 12 days 
> ago.  While I 
> have been able to overcome most problems either by trial and error or 
> through searches on the web, this is the first problem that 
> has seriously 
> stumped me.  I encountered a problem with sessions and 
> storing a database 
> (DB) object in a session.
> 
> 1.  I have a class called DB:
> 
> -----------------------------------------------------------
> class DB {
> 
> var $link_id;
> 
> //Constructor
> function DB () {
> 	$db_name = "zzz";
> 	$host = "localhost";
> 	$user = "xxx";
> 	$password = "yyy";
> 
> 	$this->link_id = mysql_connect($host,$user,$password);
> 	$db_connect = mysql_select_db($db_name, $this->link_id);
> }
> 
> function getLinkID() { return $this->link_id; }
> 
> }
> -----------------------------------------------------------
> 
> I also have other functions in this class, but have omitted them for 
> brevity's sake.
> 
> 2.  In my file login.php, which handles logins to the system I am 
> developing (simple small business application), the following 
> statements 
> are made:
> 
> -----------------------------------------------------------
> include_once("class_DB.inc.php");
> ...
> $db = new DB();
> ...
> if ($successful_login) {
> 	...
> 	session_start();
> 	session_register("db");
> 	...
> }
> ...
> -----------------------------------------------------------
> 
> I want to keep a copy of the DB object as part of the session 
> for later 
> use, as my system will be making frequent calls to the DB.
> 
> 3.  Now this is where the problem lies...
> a.  If I hit the "Submit" button on my login page it takes me back to 
> login.php, and I can access the DB object through $_SESSION["db"].
> b.  But once I move to another page and start a session 
> there, I can access 
> all session variables I have registered so far including $db, 
> except the 
> $db object now seems to be empty.
> 
> 4.  To overcome this problem I have simply stopped storing 
> the DB object in 
> the session, and now instead instantiate a new DB object on 
> each page that 
> it is necessary.
> 
> 
> So my questions then are...
> 
> [1]  Why is the DB object empty in the other pages when all my other 
> variables still maintain their values?
> [2]  Is there any "trick" to storing objects in sessions?
> [3]  Is it efficient to store whole objects in sessions?
> [4]  Is it any more efficient to instantiate the objects as 
> needed (be it 3 
> times in a session or a 100) rather than storing it once as a 
> session variable?
> 
> Thanks in advance!!
> 
> Vinny
> 
> 
> 

  Return to Index