Jump to content

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.
Link to comment
https://forums.phpfreaks.com/topic/6104-connection-problems/
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.

Link to comment
https://forums.phpfreaks.com/topic/6104-connection-problems/#findComment-21983
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.
Link to comment
https://forums.phpfreaks.com/topic/6104-connection-problems/#findComment-21989
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.
Link to comment
https://forums.phpfreaks.com/topic/6104-connection-problems/#findComment-21995
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.