class w/ database handler to otehr docs

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
PingLeeQuan
Forum Commoner
Posts: 58
Joined: Tue Sep 03, 2002 8:08 am

class w/ database handler to otehr docs

Post by PingLeeQuan »

Hello... I have a class that connects to a database and opens it. The class is "require" in my index.php
What i am trying to do is use the "handler" ( the connection to the database that i have in index.php) in another document called, getdata.php where i actually need to READ the tables and fetch the result.

I know i can access regular variables through $_POST... and/or $linkid....

Does this apply also to what i am trying to do? or do i need to establish teh connection again?


thanks in advance
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

you have to have that class include file in the other document as well

so if you include the database class file in index.php you have to also include it to the other pages that will be using the database connection.
PingLeeQuan
Forum Commoner
Posts: 58
Joined: Tue Sep 03, 2002 8:08 am

Post by PingLeeQuan »

wouldn't that reinstanciate my class again? I am trying to have 1 instanciation per session.

thanks jim
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post by samscripts »

Hi,you need to re-connect to the database in every script.

ie. every time someone browses a page on your site that accesses the database, that script needs to open a new connection.
I am trying to have 1 instanciation per session
do you mean each visitor to your site has only one database connection made, and you would store this in the session variable? This won't work, and would be a waste of resources as well, even if you could do it.

just include the connection class (include() or require()) in each script you need the database connection in.

hope this helps, Sam
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Class objects, by default, can not be passed between pages using session variables. If you provide the __sleep and __wake magic functions, then the class can be serialized across the session. However. even with serialization its a very bad idea to store the database connection and it won't work. You could, however, write your __wake function to re-establish the db conenction on deserialization so it appears transparent.
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Persistent Database Connection

Post by gite_ashish »

hi,

a must read link:

Chapter 23. Persistent Database Connections

regards,
PingLeeQuan
Forum Commoner
Posts: 58
Joined: Tue Sep 03, 2002 8:08 am

Post by PingLeeQuan »

Thanks guys... that helped greatly... :)
Post Reply