TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-12-2010, 05:04 PM   #1 (permalink)
The Contributor
 
pipesportugal's Avatar
 
Join Date: May 2008
Location: Oporto-Portugal
Posts: 32
Thanks: 11
pipesportugal is on a distinguished road
Default Connecting to database everytime You make an instance

Hello dear colleagues from the TalkPHP forum,

I am trying to build a class to read the content of all my tables.
I am calling this class record.

Everytime I create an instance of this class I am sending all the database information on the "new" sentence, meaning:

PHP Code:

$customer 
= new record("$hostname""$user_db""$pass_db""$database","$record_to_pick","$table_name"); 
Inside the class I am performing everytime all the normal operations of accessing a database, meaning:

PHP Code:

public function __construct($host,$username,$password,$db,$recordtopick,$tablename)
{
$this->host         $host;
$this->username     $username;
$this->password     $password;
$this->db           $db;
$this->recordtopick $recordtopick;
$this->$tablename   $tablename;
$this->conectar();


private function 
conectar()
{
$this->link mysql_connect($this->host$this->username$this->password);
mysql_select_db($this->db$this->link);

Most of the times, even in a very small program I am using this class many times to read several tables.

My questions are:
1) Is the fact of using this class so often, therefore connecting to the database so many times, making my programs slow down?
2) If so, how can I improve my programs?

Thanks in advance for all replies,
PP
pipesportugal is offline  
Reply With Quote
Old 06-13-2010, 06:55 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by pipesportugal View Post
Hello dear colleagues from the TalkPHP forum,

I am trying to build a class to read the content of all my tables.
I am calling this class record.

Everytime I create an instance of this class I am sending all the database information on the "new" sentence, meaning:

PHP Code:

$customer 
= new record("$hostname""$user_db""$pass_db""$database","$record_to_pick","$table_name"); 
Inside the class I am performing everytime all the normal operations of accessing a database, meaning:

PHP Code:

public function __construct($host,$username,$password,$db,$recordtopick,$tablename)
{
$this->host         $host;
$this->username     $username;
$this->password     $password;
$this->db           $db;
$this->recordtopick $recordtopick;
$this->$tablename   $tablename;
$this->conectar();


private function 
conectar()
{
$this->link mysql_connect($this->host$this->username$this->password);
mysql_select_db($this->db$this->link);

Most of the times, even in a very small program I am using this class many times to read several tables.

My questions are:
1) Is the fact of using this class so often, therefore connecting to the database so many times, making my programs slow down?
2) If so, how can I improve my programs?

Thanks in advance for all replies,
PP
It shouldn't be a problem, use mysql_pconnect to pool your connection so that don't have to continually reopen connections. Basically what that function does it it looks for a presistant connection to open with before creating one.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-13-2010, 09:26 PM   #3 (permalink)
The Contributor
 
pipesportugal's Avatar
 
Join Date: May 2008
Location: Oporto-Portugal
Posts: 32
Thanks: 11
pipesportugal is on a distinguished road
Default

Hi there VI,

I followed the link You gave me just to arrive to another link on the php.net website that simply disadvices people from using pconnect (persistent connections).

I was decided to change my instruction to mysql_pconnect(), but after reading the contents of that LINK, it made me think a lot, and also I have never really had a problem with my using of mysql_connect(), so I will stick to it.

Thank You for Your suggestion though,
PP
pipesportugal is offline  
Reply With Quote
Old 06-13-2010, 11:26 PM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Can you elaborate on 'using this class many times'? Are you using it many times in a single page view, or just many times across the site as a whole?

If you are calling the class numerous times inside of what would be a single page view, then you may benefit from creating a singleton instance of the database class. This would avoid connecting to the database more than once per page view. If you are simply calling the class once on various pages of the site, then I wouldn't worry about it at all. Most applications create a new connection on every page load.
delayedinsanity is offline  
Reply With Quote
Old 06-14-2010, 03:47 AM   #5 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by pipesportugal View Post
Hi there VI,

I followed the link You gave me just to arrive to another link on the php.net website that simply disadvices people from using pconnect (persistent connections).

I was decided to change my instruction to mysql_pconnect(), but after reading the contents of that LINK, it made me think a lot, and also I have never really had a problem with my using of mysql_connect(), so I will stick to it.

Thank You for Your suggestion though,
PP
I am not intimately familiar with persistent connections on a mysql server, but when I used them on an IIS server (running SQL Server 2005) it fixed the connection errors we were having. Perhaps Microsoft has done a better job implementing them.

As long as your class is not opening and closing the connection on each query, a large number won't be a problem. But opening and closing them repetitively will cause trouble for you if you get decent traffic.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-14-2010, 01:47 PM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

The main problem with persistent connection (as I recall) is that they persist beyond the execution of the script, thus if you don't close them properly after you have finished with them you will cause all of the connections in the pool to be used up and therefore giving you a 'Too many connections' type error (im sure its more in depth than that, but I'm at work atm and can't really spend any time to research it). If I remember persistent connections are useful for keeping connections open when the overhead of opening connections is large (i.e. different server for mysql than web server etc) and a few other things, but usually you can avoid using them by better structuring your solution to the problem.

This problem may be better solved by a better program design, i.e. abstracting the DB stuff to another object that persists throughout the execution and can be passed around using dependency injection to objects that need it (you could even make it a singleton if needs be, as suggested by delayedinsanity).
PHP Code:
$pDb = new DBConnection('localhost''user''pass''db');

$pRecord1 = new Record($pDb$RecordID);
$pRecord1->doSomethingWithTheDB();

$pRecord2 = new Record($pDb$RecordID);
$pRecord2->doSomethingWithTheDB(); 
Or have I missed the problem?
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 06-15-2010 at 09:04 AM. Reason: incorrect variable name in example
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the Singleton design pattern Karl Advanced PHP Programming 27 10-22-2012 08:16 AM
Question | There is a way to make merged login config.php?? (MSSQL+MySQL) FrozenGod Absolute Beginners 1 11-06-2009 07:52 AM
Using the factory pattern (mad rantings of a mind without coffee) sketchMedia Advanced PHP Programming 35 09-25-2009 11:05 AM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Adding Images to a database from a folder Rendair Advanced PHP Programming 3 01-13-2008 07:40 PM


All times are GMT. The time now is 08:36 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design