How to organize the processing of database connections inside functions?

When I wrote PHP code with PDO and MySQL, I always created connections when I needed it, as part of such functions:

result pseudo_function() {
  create and open connection
  do stuff
  close connection
  return result
}

Now I started programming in C, I saw that pointers are an interesting way to pass an entire connection as a parameter to a function. I was wondering if it would be better to pass connections between functions until the entire user request has been submitted.

To clarify: for a single user request there can be 1-5 calls to a function that then opens the database, retrieves data, does something, closes and returns.

Does performance difference also matter if you support an open connection?

+3
source share
3

5 , .

singleton; , .

+3

If you are developing web applications using PHP, it is common (and, I believe, the most efficient way) to open a database connection once and close it when the script completes. Maintaining communication while other material is being executed does not actually create any overhead or require no action at all, but reconnects each time.

+2
source

All Articles