Solved

proper use of DB::connect using PEAR

Posted on 2006-07-14
3
642 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do i connect MySQL on Visual Studio 3 40
Problem with SqlConnection 5 124
Selecting specific rows 3 36
php image upload 3 27
Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now