NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page One MYSQL connection per function or big no no?

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 11-11-2009, 02:58 PM THREAD STARTER               #1 (permalink)
NamePros Regular
 
omegapandor's Avatar
Join Date: May 2008
Posts: 246
omegapandor has a spectacular aura aboutomegapandor has a spectacular aura about
 



One MYSQL connection per function or big no no?


I'm currently learning how to make PHP and MYSQL be buddies. So far I've created a connection function and I use it in every other function in order to shorten the code.

Since I'm connecting to the MYSQL server (service on the apache server?) every single relevant function call, and I making huge overhead? Or is this normal behavior for talking with MYSQL?

Should I just call the connect function, run my other functions, and then close?

Thanks.
__________________
Are you tired of viruses and popups? Use Firefox!
omegapandor is offline   Reply With Quote
Old 11-12-2009, 12:53 AM   #2 (permalink)
NamePros Member
 
adamo's Avatar
Join Date: Aug 2008
Location: poland
Posts: 94
adamo is an unknown quantity at this point
 




Theres no need to open up the connection for every function if you use them one after another.
Usually you can connect per script or if you use classes in PHP The Singleton Design Pattern for PHP
__________________
adamo is offline   Reply With Quote
Old 11-16-2009, 08:03 AM   #3 (permalink)
NamePros Member
 
rafaelrls's Avatar
Join Date: Jan 2008
Location: me = new (live(in.paradise())
Posts: 174
rafaelrls is an unknown quantity at this point
 



Adamo answered it well man, too many connections causes an overload on the mysql server. it is better to use just one connection per session.
rafaelrls is offline   Reply With Quote
Old 11-16-2009, 08:18 AM   #4 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Originally Posted by adamo View Post
Theres no need to open up the connection for every function if you use them one after another.
Usually you can connect per script or if you use classes in PHP The Singleton Design Pattern for PHP
I do something like that
PHP Code:
class db
{
    private static 
$instance;

    private function 
__construct(...)
    {
        
//
    
}

    public static function 
getInstance()
    {
        if (!
self::$instance)
        {
            
self::$instance = new self();
        }
        return 
self::$instance;
????: NamePros.com http://www.namepros.com/programming/622489-one-mysql-connection-per-function-big.html
    }
}

$db db::getInstance(); 
Of course there's a lot more to it than that.
Eric is offline   Reply With Quote
Reply

Tags
mysql, mysql_connect(), php


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


 
All times are GMT -7. The time now is 02:33 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger