Forum Moderators: coopster

Message Too Old, No Replies

Database connection through multiple classes

         

wavesurf

8:48 pm on Nov 23, 2006 (gmt 0)

10+ Year Member



I have just started experimenting with OOP and I seem to understand the basics. However I have a few problems that I can't seem to work out.

More than one of my classes (that is needed in the same script) needs to connect to a MySQL database. I have written a MySQL-connect class that opens up the connection and gets me a resultset. Of course I can get this to work in different classes at the same time, but it means opening up a separate connection to the database for each class.

Is there a better way to do this? Maybe by using another function than mysql_connect, or is there any way to have a global class, which can be used everywhere in the script.

Any takers?

Thanks for any help.

jatar_k

12:24 am on Nov 24, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> I have written a MySQL-connect class that opens up the connection and gets me a resultset.

there's your problem, the class is trying to do too much. You should have connect and return result seperately.

a global connect class should work

wavesurf

5:56 am on Nov 24, 2006 (gmt 0)

10+ Year Member



Well, it actually is two different classes, one MySQLConnect and one MySQLResultSet, but I have a function within my connect-class that calls the result set-class. Is there a better way to do this?

But, back to the main problem: How do I manage to make the connect class global?

wavesurf

5:57 am on Nov 24, 2006 (gmt 0)

10+ Year Member



By the way, I'm using PHP5

coopster

4:32 pm on Nov 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



How about establishing the connection through your MySQL connection/database handler class and then passing that as a value to your other classes? For some other ideas, open up the PEAR DB class and a class that uses the DB class to see how the PEAR development team handled the issue.

bleak26

5:29 pm on Nov 27, 2006 (gmt 0)

10+ Year Member



Not sure weather this is relatvent but what about mysql_pconnect [uk.php.net...] For a persitent conncetion.