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
Switch to threaded view of this topic Create a new topic Submit Reply
Best PHP way to connect to a DB across multiple pages ? [message #169477] Tue, 14 September 2010 22:28 Go to next message
goldtech is currently offline  goldtech
Messages: 5
Registered: September 2010
Karma: 0
Junior Member
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.
Re: Best PHP way to connect to a DB across multiple pages ? [message #169478 is a reply to message #169477] Tue, 14 September 2010 23:48 Go to previous messageGo to next message
Hamish Campbell is currently offline  Hamish Campbell
Messages: 15
Registered: September 2010
Karma: 0
Junior Member
On Sep 15, 10:28 am, goldtech <goldt...@worldpost.com> wrote:
> 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?

Yes, it is correct.

You can create persistent database connections if you really need to
(see <http://php.net/manual/en/features.persistent-connections.php>),
but unless you have a specific requirement it is best to stick to non-
persistent connections.

Regards

Hamish
Re: Best PHP way to connect to a DB across multiple pages ? [message #169480 is a reply to message #169477] Tue, 14 September 2010 23:56 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
goldtech 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?
>

More or less yes.

I dont think the overhead of setting up the connection is really
significant.


> Thanks,
>
> Lee G.
>
>
Re: Best PHP way to connect to a DB across multiple pages ? [message #169483 is a reply to message #169480] Wed, 15 September 2010 01:44 Go to previous messageGo to next message
Beauregard T. Shagnas is currently offline  Beauregard T. Shagnas
Messages: 154
Registered: September 2010
Karma: 0
Senior Member
The Natural Philosopher wrote:

> goldtech wrote:
>> 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?
>
> More or less yes.
>
> I dont think the overhead of setting up the connection is really
> significant.

...and don't forget to close the connection immediately after you are
done with it.

mysql_close($dbc);

--
-bts
-Four wheels carry the body; two wheels move the soul
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 next message
Gordon[1] is currently offline  Gordon[1]
Messages: 6
Registered: September 2010
Karma: 0
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.
Re: Best PHP way to connect to a DB across multiple pages ? [message #169520 is a reply to message #169490] Thu, 16 September 2010 17:58 Go to previous messageGo to next message
goldtech is currently offline  goldtech
Messages: 5
Registered: September 2010
Karma: 0
Junior Member
Thank You to posters for their time and effort in answering my
question !
Re: Best PHP way to connect to a DB across multiple pages ? [message #169521 is a reply to message #169490] Thu, 16 September 2010 17:59 Go to previous message
goldtech is currently offline  goldtech
Messages: 5
Registered: September 2010
Karma: 0
Junior Member
Thank You to posters for their time and effort in answering my
question !
  Switch to threaded view of this topic Create a new topic Submit Reply
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 03:01:58 GMT 2024

Total time taken to generate the page: 0.02451 seconds