Jump to content

Archived

This topic is now archived and is closed to further replies.

Javizy

Connection Problems

Recommended Posts

I'm developing a ShopManager class which uses a Basket class to store items. The ShopManager class needs to perform certain database actions like getProducts, getProduct, and so on.

I have a simple Connection class which has functions executeQuery(), and close().

Products and basket items are displayed in files shop.php and basket.php.

Originally I was coding as follows in the ShopManager functions

[code]$conn = new Connection(); /* Makes a connection automatically */
$conn->executeQuery($sql);
$conn->close();[/code]
However, several function calls may be required per php page. Since I'm not able to use pconnect this is a problem; I don't want to incurr multiple connection costs on a single page load. So I coded shop.php like follows:

[code]$conn = new Connection();
$manager->getProducts();
$manager->basket->addItem();
$conn->close();[/code]
Having connection logic in the "view" pages is going to make things less maintainable, reusable, etc, so I would appreciate any alternative suggestions.

On a side note, could anyone explain access modifiers with PHP4? Use of private, public, etc doesn't seem to be supported until v5.

Share this post


Link to post
Share on other sites
Technically, you don't even have to close the mysql connection, but your second method is definately the better of the two. There is no problem with leaving the connection open for the entire page. Subsequent query calls are just going to reuse the existing connection.

Share this post


Link to post
Share on other sites
[!--quoteo(post=359697:date=Mar 29 2006, 05:28 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 29 2006, 05:28 PM) [snapback]359697[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Technically, you don't even have to close the mysql connection, but your second method is definately the better of the two. There is no problem with leaving the connection open for the entire page. Subsequent query calls are just going to reuse the existing connection.
[/quote]

What I mean is, I want all the connection stuff to be taken care of by the ShopManager\Connection class, keeping the shop.php and basket.php pages as simple (logic wise) as possible. So that any pages that need to use the ShopManager don't have to worry about connections.

Share this post


Link to post
Share on other sites
Well, I'll explain how I manage my stuff. All my connections are setup in my header file that I include with all my files. The connection is opened at the top of the page and then I have a class that I use to run the actual queries. I never close the connection, it just drops off automatically. So I never really write a "open connection" call in my pages. It's always just there. You obviously can't avoid calling queries within your pages, so you have to do that stuff.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.