> -----Original Message-----
> From: tedd [mailto:tedd.sperl...@gmail.com] 
> Sent: Tuesday, May 05, 2009 7:55 AM
> To: php-general@lists.php.net
> Subject: Re: [PHP] Avoid to open mysql querries then times in the page
> On 5/4/09, Matthieu <spama...@gmail.com> wrote:
> >  Hello,
> >
> >  I'm a totally newbie to php/Mysql but I'd like to know if 
> it is normal that
> >  I have to connect 3 times to the db in one page.
> >
> >  For example, I have
> >
> >  1. A connection for the login / pass a $_SESSION['login'] 
> before the HTML
> >  tags
> >
> >  2. I need to say hello to the user so I reconnect, run a 
> query to select the
> >  user having the login and echo 'Hello '.$user['login'].'!''
> >
> >  3. I need to show him his friends a bit later, so I have 
> to connect a last
> >  time and re-run a querry because I can't use the data 
> $user that I used in
> >  my upper php code...
> >
> >
> >  Is there a walkthroug to have only one connection for the page?
> >
> >  Thanks
> >
> >  Matthieu

I usually include a db.inc.php file that opens up a connection in the header
of the page and I continue to use the connection throughout till the end.

A basic example is do something like this:

//this will hold each db connection so we'll only create one at a time. like
a singleton.

function connect_to_db_reliability() 
        if ($GLOBALS['DB_CONNECTIONS']['reliability']) 
                return $GLOBALS['DB_CONNECTIONS']['reliability'];

        global $global_db_dsn_reliability;

        $options = array(
                'debug'   => 0,
                'persistent'  => FALSE,
                'portability' => DB_PORTABILITY_ALL

        $db_connection =& DB::connect($global_db_dsn_reliability, $options);
        if ( PEAR::isError( $db_connection ) )
                die( $db_connection->getMessage() );

        $GLOBALS['DB_CONNECTIONS']['reliability'] = $db_connection;

        return $GLOBALS['DB_CONNECTIONS']['reliability'];

Then in all your code you can either do a simple call at the top of your

        $db_connection = connect_to_db_reliability();

and later on, you don't have to worry about it because you have already made
a connection.

        $db_result =& $db_connection->getAll($sql);

Be aware of scope and such, for example if you're inside a function or
method. But that's easy enough to handle since calling $db_connection =
connect_to_db_reliability(); will return you the same connection anyways, so
it's free to use wherever you are unsure.

This method is a fairly simple "singleton" like methodology that handles
multiple database connections (here at my job, we have NINE databases we
connect to at any given time.

In my other jobs/projects where I only have a single database (which is the
norm), I do this same sort of logic just not in a multi-dimensional array
and I have more wrapper functions that handle the connection if it doesn't
exist and do all sorts of useful things like colorized debug output,
backtraces. Search the PHP list archives for my posts and SQL_QUERY,
SQL_CONNECT, etc in a "db.inc.php" file I shared...


PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to