Re: PHP Connections

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



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.



[Index of Archives]     [Postgresql General]     [Postgresql Admin]     [PHP Users]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Databases]     [Yosemite Backpacking]     [Postgresql Jobs]

  Powered by Linux