Well, I’m relatively new to php but have worked on web apps previously.
I’m currently writing code in php and running Apache httpd. The DB I’m using is PostgreSql.
This web app will connect to DB very frequently. For that reason I’m looking for something that enables me not to initiate connection every time a user accessed a page.
Is it possible in php to set a variable that is same for all users.
For eg. I want to save a DB connection object in a variable and use it whenever I want.
That variable will go out of scope when Apache Server stops.
Smarmy answer first. Yes you can. It’s called the database 😉
Seriously though, Apache + PHP (without anything else) is not the best option if you’re trying to persist values across requests. Normally, if that type of functionality is needed, it is done through either a hard file or a database connection which is refreshed every time a request is triggered. Obviously neither of those are sufficient to persist an entirely separate connection.
The general rule is one request one connection. There are ways to persist connections so that this number is lowered but
pg_connectwhich was supposed to be a canonical way to approach this problem, seems to be incredibly broken. You may wish to look intopgbouncerorpgpoolinstead. I don’t know how the PDO driver handlesATTR_PERSISTENTfor Postgre.