Re: PHP Connections

Поиск
Список
Период
Сортировка
От scott.marlowe
Тема Re: PHP Connections
Дата
Msg-id Pine.LNX.4.33.0308051246040.13758-100000@css120.ihs.com
обсуждение исходный текст
Ответ на PHP Connections  ("David Busby" <busby@pnts.com>)
Список pgsql-php
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.


В списке pgsql-php по дате отправления:

Предыдущее
От: Lynna Landstreet
Дата:
Сообщение: Re: PHP Connections
Следующее
От: "scott.marlowe"
Дата:
Сообщение: Re: PHP Connections