0

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.

5
  • You realize that PHP scripts start an entire new lifetime each time the website is called? Even global variables only live for one page visit. What's the exact design goal here? Commented Sep 4, 2011 at 1:27
  • Create required db connections and run all prepared queries on that connection. Save that connection object later :) Commented Sep 4, 2011 at 1:43
  • @Mayank: how many connections are we talking about? Since most of the time you wouldn't have an issue with lots of connections. Commented Sep 4, 2011 at 1:54
  • @PeeHaa Creating connection everytime (as far as I understand) is a overhead. Moreover, I'm always in favor of running prepared queries rather than normal. So, its matter of creating connections no. of times instead how many. I want to create a connection pool in Apache Server memory if possible. Commented Sep 4, 2011 at 1:57
  • @Mayank: I don't think you have to worry about the connections. If you just setup 1 connection for every request (e.g. in your bootstrap file) you will be fine. And also what you are trying to do is simply not possible. PHP is made in a way that 'nothing' can be shared. So 1 connection == 1 request Commented Sep 4, 2011 at 2:02

2 Answers 2

2

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_connect which was supposed to be a canonical way to approach this problem, seems to be incredibly broken. You may wish to look into pgbouncer or pgpool instead. I don't know how the PDO driver handles ATTR_PERSISTENT for Postgre.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm looking for a solution where I can save db connection objects. This solution will save a db connection object into a DB and to retrieve that object I've to connect to DB :)
@Mayank: It might be possible that you have a bit of a wrong idea how PHP and databases interact. Maybe you could make the question more concrete and specific, and something will suggest itself.
@Kerrek I'm aware of the way PHP interacts with backend. I just tried my luck here if I might get something. Thanks all for your help. I found pgpool pretty interesting; will look into it more
Well, does the global variable stay alive for the session? I'm creating a global array in one.php and at the end of the file calling the init() function to initialize that global variable. init function will have logic to initialize it only once. I want this global variable across different pages. Does it need anything special? Is it (not) a recommended practice?
0

The need to connect to a DB frequently is a common feature of web applications written in PHP, which is why the oci_pconnect() function uses a persistent cache of connections that can be re-used across different script/page requests (so the DB connection overhead is paid only once per Apache child or PHP process) - see PHP DB connection handling

Is there any particular reason why you want to do the work of managing a DB connection pool yourself? If you're absolutely Hell-bent on this idea, then you could use something like a shared memory segment to store data objects that are to be shared across processes - but then you need a mutex system to avoid death by race condition when modifying the shared data, a "watchdog" to clean things up when a process dies without properly releasing a mutex, etc.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.