Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

proper use of DB::connect using PEAR

Posted on 2006-07-14
3
652 Views
Last Modified: 2012-05-05
i am implementing a medium size app in php using mysql/PEAR.  it's design is loosely based on the orielly book book "php and mysql".  yes, i'm pretty new at mysql.

each page of the authors example app begins with $conn = DB::connect($connString,true) [sic] but there is no $conn->disconnect().   sometimes my app gets a compliant (error) that i have exceeded too many connections.

in looking at the PEAR doc, i believe the 2nd param in the connect() call is incorrect, but my original coding had it that way which resulted in no complaints (???))

assuming one script per page of html output page:
1.  is it proper usage to DB::connect() in each script?
2. can i save the object returned from that connect() and use it on other pages/scripts?  is that the right thing to do?
3. or should each  page/script start with DB::connect() and end with disconnect()?

saving the connection object like this
   $_SESSION["connection"] = DB::connect($connStr)
then using it later, like this
   $query = "SELECT blah blah blah..."
   $result = $_SESSION["connection"]->query($query)
seems to work on some pages (in my development environment, anyway, using php4 - the actual hosting site uses php5)

thoughts?

thanks
0
Comment
Question by:prevostpilot
  • 2
3 Comments
 
LVL 8

Expert Comment

by:Autogard
ID: 17113355
>>1.  is it proper usage to DB::connect() in each script?

Yes, that works fine.

>>2. can i save the object returned from that connect() and use it on other pages/scripts?  is that the right thing to do?

Actually, in PHP database connections are meant to last the life of a page and a new connection should be established with each page.  In older versions of PHP they actually had something called a persistent connection which could be used over various pages, but I think they are phasing it out in the newer versions.

>>3. or should each  page/script start with DB::connect() and end with disconnect()?

There really isn't much need to use the same connection on another page.

# Somewhere near the beginning
$dbconn = DB::connect($dbsettings);
if(PEAR::isError($dbconn))
{
      die($dbconn->getMessage());
}
else
{
        # Code here

        # Somewhere at the end
        $dbconn->disconnect();
}
0
 

Author Comment

by:prevostpilot
ID: 17115080
Autogard -

thanks for the quick response.  just one last clarification:

>> >>3. or should each  page/script start with DB::connect() and end with disconnect()?
>> 
>> There really isn't much need to use the same connection on another page.

is the disconnect() needed?   if not, what causes the disconnect? "inactivity" timeout? does mysql timeout and disconn or does PEAR do the disconn?

a day or two later i ran the exact same app (with a connect at the top of each script), and had no problem.
why do you suppose i got the "too many connections" error?  thnking back, i'm pretty sure the error came from my
  if (DB::isError($result))
            trigger_error($result->getMessage(), E_USER_ERROR);   // trigger_error() is a custom handler
tho i could be mistaken at this point - perhaps it came from within PEAR - cant be sure as i didn't save the error msg.

tnx

0
 
LVL 8

Accepted Solution

by:
Autogard earned 150 total points
ID: 17115864
>>is the disconnect() needed?

From: http://opensource.apress.com/article/36/a-quick-tutorial-of-pear-db
DB::disconnect
boolean disconnect ()
This is, obviously, the function to use when disconnecting from a database. It is always good practice to use the disconnect function. In PHP, the end of a script should automagically do a disconnect, but I wouldn’t rely on it. Use it just to be safe.
To use this function, we will reference the database object that was returned from an inital connect statement:
$db = DB::connect($dsn, false);
$db->disconnect();

Like it says the disconnect "should" occur, but maybe wasn't in your script.  Always safe, and good practice to put the actual disconnect call there.  If it doesn't disconnect then the mysql timeout should kick in.  Check your "connect_timeout" setting for mysql.  But if you get a lot of connections you can get to your limit real quick.  There is a mysql variable that controls max connections too.

Official doc for disconnect is here: http://pear.php.net/manual/en/package.database.db.db-common.disconnect.php
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question