Jump to content


Photo

Connection Problems


  • Please log in to reply
3 replies to this topic

#1 Javizy

Javizy
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 29 March 2006 - 04:13 PM

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

$conn = new Connection(); /* Makes a connection automatically */
$conn->executeQuery($sql);
$conn->close();
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:

$conn = new Connection(); 
$manager->getProducts();
$manager->basket->addItem();
$conn->close();
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.

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 29 March 2006 - 04:28 PM

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.


Info: PHP Manual


#3 Javizy

Javizy
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 29 March 2006 - 04:38 PM

[!--quoteo(post=359697:date=Mar 29 2006, 05:28 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 29 2006, 05:28 PM) View Post[/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.

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 29 March 2006 - 04:49 PM

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.

Info: PHP Manual





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users