FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Best PHP way to connect to a DB across multiple pages ?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Best PHP way to connect to a DB across multiple pages ? [message #169490 is a reply to message #169477] Wed, 15 September 2010 09:21 Go to previous messageGo to previous message
Gordon[1] is currently offline  Gordon[1]
Messages: 6
Registered: September 2010
Karma:
Junior Member
On Sep 14, 11:28 pm, goldtech <goldt...@worldpost.com> wrote:
> Hi,
>
> I have a basic PHP and MYSQL connection question/reality check.
>
> Let's say I have two pages which rely on getting content from the same
> DB. The first page shows all the records from the DB in a brief
> tabular format. If the user sees something they are interested in they
> click a link in a displayed brief record and go to the second page
> which displays a detailed view of the item they clicked on with a
> bigger picture and more info.
>
> This is standard, tons of web pages have this format. Both pages pull
> info from the same DB using PHP. Let's say I post the primary key of
> the item from the brief page and send that key to the detail page. The
> detail page uses the key to access the DB again and get more detailed
> content for that record.
>
> On both pages I access the DB with:
> ...
> $dbc=@mysqli_connect (...... );
> $q = "SELECT.... FROM data";
> $r = @mysqli_query($dbc, $q);
> ...
>
> So I'm make a connection twice, once per each page that uses data from
> the DB. Always done it this way. Question:  Is this a correct way to
> do this?
>
> Thanks,
>
> Lee G.

Unless the amount of traffic the web app gets is absolutely enormous,
the overhead of database connection isn't worth worrying about. Just
remember to connect once at the start and to close it when you've
fetched all the data you need for the goal you're trying to achieve.
Don't disconnect and reconnect again during the course of a script as
that means you're causing more overhead than is strictly necessary.

If the web app in question does get an enormous amount of traffic then
the database connection overhead can become an issue. PHP does
provide a persistent connections mechanism that can keep a database
connection alive and recycle it for subsequent use. However, this
mechanism comes with some important caveats and from what I've read of
the technique it could prove to be more trouble than it's worth.
Other approaches, such as memcached or putting the database on the
same machine as the web server and connecting to it through a pipe
instead of over TCP/IP might be more appropriate, though the latter
may also be problematic if you have a setup that shares a single
database over multiple web server boxes.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Reference # in var_dump output?
Next Topic: OOP, classes and databases
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 22:39:49 GMT 2024

Total time taken to generate the page: 0.04357 seconds