Link to home
Start Free TrialLog in
Avatar of prevostpilot
prevostpilotFlag for United States of America

asked on

proper use of DB::connect using PEAR

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
Avatar of Autogard
Autogard

>>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();
}
Avatar of prevostpilot

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Autogard
Autogard

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial