Click here to Skip to main content
14,303,949 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello all,

A very novice question...

I start to understand how PHP works...

I don't know if this is a mistake, but my idea was to connect to the MySQL database at the beginning and use that connection till the end.

I guess I should put the connection in a SESSION variable to get this behavior...

The other way would be to simply create a connection each time I want to make a query to the database and close it after doing the query... this makes things easier from the programming point of view...

How this should be done?

Thank you for your answer!

What I have tried:

Tried to create a class and make queries to that class, but this seems to work only sometimes...
Updated 23-Jul-19 7:40am
Afzaal Ahmad Zeeshan 23-Jul-19 14:16pm
Consider looking out for connection pooling in this case.
Joan M 23-Jul-19 14:24pm
I'll do it only if I see the web page is slow... by now... a small database with 11 tables... don't think this will be needed...

But I'll check it... thank you Afzaal!

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Create the connection when you need it, and close it as soon as you're done.

The web is stateless. Clients make a request and get a response. They might never make another request again. Don't try to persist expensive things like database connections between requests.
Joan M 23-Jul-19 13:46pm

I had created a class with the connection as a member variable... too much things to get a simple thing done.

Tried it and worked like charm at the first attempt.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100