Global Database Connection?

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
montecristo
Forum Newbie
Posts: 20
Joined: Thu Oct 19, 2006 9:51 am

Global Database Connection?

Post by montecristo »

Hi guys,

I have just started PHP, so please forgive me if my question is stupid. I am developing a simple customer application, where a user can log in and query a customer and the cutomer's purchases.

I have a database class which handles all database functionality e.g connects to the database etc. This class is called when a user logs on to the system. This validates the username and password, if these are corrrect it calls the query screen using

Code: Select all

header('location:queryuser.php');
Next a query screen appears allowing the user to query a customer id, which brings back the customer details from the database.

To get the details I have to call my database class again, connect and login into the database to retrieve the details.Surely there is another way to do this without creatng a new instance of the database class?

Any suggestions would be appreciated

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Each request to PHP is a separate instance, it has no relation to the previous request. So normally, a class is not able to persist across requests. You can store a class into a session variable, and there are shared memory options available too, but in this case, the normal course of action is the create another instance of the database class. I'd use session data to persist the user information so you don't have to request that they reauthenticate.

Also, relative path redirection isn't supported with some browsers, specifically standards complaint (only) ones. Use full URLs, http://domain and all..
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Storing the class in session is probably going to take up a lot of memory if you have a bunch of users on at any given time. Try to store a unique id which is assigned to each user in a session variable and then grab that info on each page load.
montecristo
Forum Newbie
Posts: 20
Joined: Thu Oct 19, 2006 9:51 am

Post by montecristo »

Thanks alot guys,

Just had a quick look at session data, seems it might do the trick.

Thanks for the quick response!
Post Reply