Click here to Skip to main content
15,885,917 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...
Posted
Updated 23-Jul-19 7:40am
Comments
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

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.
 
Share this answer
 
Comments
Joan M 23-Jul-19 13:46pm    
THANK YOU VERY MUCH.

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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900