Re: php/mysql web question
| From: | Murray @ PlanetThoughtful | Date: | Tue, 06 Sep 2005 19:12:56 +0000 |
| Subject: | Re: php/mysql web question | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-221921@lists.php.net to get a copy of this message | ||
> hi...
>
> if an app has a webpage that has to interface/display data from a mysql
> db,
> does the app have to essentially do a new db_connection for each time that
> a
> user accesses the page during the session.
>
> as far as i can tell, it does.
>
> in other words, the page would need to look something like:
>
> <?
> start the page
> do the db_connect
> do the db_query
> process the information from the db/tbl
> close the db_connection
> ?>
>
> the only reason i ask is that it would be nice/good if there was some way
> of
> opening a connetion once for a given session (save bandwidth) as well as
> somehow saving data from the db/tbl (short of using session vars)
Hi Bruce,
Basically what you're asking about is persistent connections from PHP to
MySQL, which are performed using mysql_pconnect().
In the pseudo-code outline you have above, you don't need to perform the
'close the db_connection' step. Each time a page loads that has a
mysql_pconnect statement, it will check to see if an existing (or persisted)
connection is available, and will use it instead of creating a new
connection, if one does. If no persisted connection is found to be
available, mysql_pconnect goes ahead and creates the connection, and
persists it for future connection attempts.
Each page should still probably perform the mysql_pconnect function, since
unless you have a very statically defined interaction of pages, you may not
be able to guarantee that a connection already exists when a particular page
is loaded.
Don't forget that while closing the db connection isn't necessary on a
page-by-page basis when using persistent connections, you should still
habitually free recordsets either at the bottom of the page, or at a point
in your code where you know they are no longer needed.
Hope this helps a little.
Much warmth,
Murray
---
"Lost in thought..."
http://www.planetthoughtful.org