pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: scott.marlowe <scott.marlowe@ihs.com>
To: David Busby <busby@pnts.com>
Cc: pgsql-php@postgresql.org
Subject: Re: PHP Connections
Date: Tue, 5 Aug 2003 12:51:06 -0600 (MDT)
Message-ID: <Pine.LNX.4.33.0308051246040.13758-100000@css120.ihs.com> (raw)
In-Reply-To: <009601c35b79$48810010$1100000a@busbydev>

On Tue, 5 Aug 2003, David Busby wrote:

> List,
>     Which way to connect is better for my scripts?
> 
> a global
>   $db = pg_connect('asdfasdfasdfasdf');
> and every function can have 
>   global $db;
> at the top?
> 
> or like this?
> 
> function db_handle()
> {
>   return pg_connect('asdfasdfasdfasdfadsf');
> }
> 
> and everyplace needed use `db_handle()` so I call pg_exec like
> 
> $rs = pg_exec(db_handle(),"select everything from everywhere"));
> 
> So does that db_handle() make a new connection each time?
> I'm really looking for the way to optimise my connection usage.

All you're really doing here is wrapping your connects in a function.  
since you (should) only connect at the top of the script, this function 
will only be called once, and the difference in performance is negligable.  
But the gains are worth the trouble because you can then switch out common 
parts of each function you'd always do rather than cut and paste it into 
all your scripts.

Taking this a step fruther, put all your pg_xxx functions 
you'll use into an abstract layer that has them named like:

<?php
function db_connect($database){
    $conn_str = "host=w.x.y.z user=fred dbname=$dbname";
    return pg_connect($conn_str);
}

function db_query($query){
    more code here.
}
?>

Toss in some error checking and you can use those functions for things 
like controlling which server or username the scripts are connecting from.  
Then put them in the php_include path somewhere and just include_once() 
the function lib.

This allows you to "switch out" things like databases or database servers 
or accounts or what not in one fell swoop with ease.




view thread (7+ messages)  latest in thread

Message-ID: <Pine.LNX.4.33.0308051246040.13758-100000@css120.ihs.com>
Permalink:  ../Pine.LNX.4.33.0308051246040.13758-100000@css120.ihs.com/
Also on:    postgresql.org/message-id/Pine.LNX.4.33.0308051246040.13758-100000@css120.ihs.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: scott.marlowe@ihs.com, busby@pnts.com
  Subject: Re: PHP Connections
  In-Reply-To: <Pine.LNX.4.33.0308051246040.13758-100000@css120.ihs.com>
  References: <009601c35b79$48810010$1100000a@busbydev> <Pine.LNX.4.33.0308051246040.13758-100000@css120.ihs.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox