Re: MySQL and SESSIONs

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

 



On 10/5/07, Stut <stuttle@xxxxxxxxx> wrote:
>
> Vo, Lance wrote:
> > What's good for multiple webservers? thanks
>
> * DO NOT USE PERSISTANT CONNECTIONS
>
> * Minimise the amount of time you keep a database connection open during
> a request. Good logic/presentation separation helps a lot here.
>
> * Cache the crap out of everything - don't hit the DB unless you really
> need to.
>
> -Stut
>
> --
> http://stut.net/
>
> > -----Original Message-----
> > From: Stut [mailto:stuttle@xxxxxxxxx]
> > Sent: Friday, October 05, 2007 4:31 PM
> > To: Stefano Esposito
> > Cc: php-general@xxxxxxxxxxxxx
> > Subject: Re:  MySQL and SESSIONs
> >
> >
> > Stefano Esposito wrote:
> >> is it somehow possible to store the connection reference obtained from
> mysql_connect() (note the absence of the "i") in a $_SESSION element?
> >
> > No. Why would you want to? You'd end up holding on to a database
> > connection even when nothing is using it. If you want to optimise things
> > look at http://php.net/mysql_pconnect but bear in mind that this starts
> > to suck if you scale up to multiple web servers.
> >
> > -Stut



Hi. This may not be best practice, but I think it's great to use -
especially since I can use multiple functions with the same database
connection w/o having to send the db link/resource. Store the connection in
the GLOBALS variable - this way, it's accessible by each function AND is
destroyed when the script completes (e.g., when the processing is complete
for the current page).

[code]
connect.php:
$_GLOBALS['dbLink'] = mysql_connect (DATABASE, USER, PASS);
...

functions.php:
function someFunction () {
    ....
    $result = mysql_query ($query, $_GLOBALS['dbLink']);
    ....
    // Don't close the connection
}

somePage.php:
$something = someFunction();
...
mysql_close ($_GLOBALS['dbLink']); // End of page - close connection
[/code]

That's the general gist of it. BTW, if this is bad practice or there is
something more efficient, please let me know. =P

~Philip

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux