connection from one page to another

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a class that is my interface to mysql. I create an instance og  
class an connect to db. Then i change to another page and want to use  
the same instace of class. How can i send an object (the instance) to  
another page ?

Any help will be appricated.

Re: connection from one page to another

That depends. If the instance is to be kept in one "build" of your page  
(your different pages are included and/or required) you can keep it in a  
(global) variable.
If it has to exist along mutiple client requests, it is another story.  
HTTP is a stateless protocol, so once the page has been sent to the  
client, all is over and forgotten. No state is kept.
Now this can be "repaired" with sessions if you use them. However, a  
session is nothing more than a storage "just in case the client comes  
back". Web applications lead a very fragmented life.
Especially a database connection is not something I would store between  
sessions. There is a major chance that the client does not come back  
(every website visitor leaves at SOME point) and you would end up  
keeping connections open that are not used anymore.

If you do want an object stored in the session, just keep it in the  
SESSION array (a superglobal). Beware that the classes have to be  
defined (and timely) in every page that can encounter such an object.

Best regards

gachsaran wrote:
Quoted text here. Click to load it

Re: connection from one page to another

Quoted text here. Click to load it

You can't store a database connection (well, the resource that is the
link) in a session anyway.  Sessions can only store data that is
serializable, of which resources are not.  If you have a class for your
database you can define the __sleep() and __wakeup() methods to
open/close the actual link when the object is serialized/unserialized.

If you want to share actual database connections among requests, the
best you can probably do is use mysql_pconnect() instead of
mysql_connect().  pconnect will check for an open link to reuse before
trying to make a new one.  At the end of the request the connection is
left open.  Be careful though...unless you really have a reason to use
pconnect() I'd stay with plain connect().

Site Timeline