tags:

views:

28

answers:

3

basically i'd like to know if it's preferable to establish a database connection before each database query, and then use mysqli_close() immediately after the relevant section, for every spot in the layout where database information has to be pulled - or if it's better to just open the database connection at the start of the file, and then use mysqli_close() near the end of the file.

A: 

it's better to just open the database connection at the start of the file, then get all the data, then use mysqli_close(), and then call a template to start displaying a page.

Col. Shrapnel
+1  A: 

One connection per request is more efficient. Only if you do many concurrent updates on the same rows is important to commit (close connection) as fast as posible.

iddqd
There's an important distinction between closing a connection and committing a transaction. If you close a connection, you have to open a new connection which takes a large amount of time compared to other operations. Starting a transaction, on the other hand, is much quicker (and indeed, usually starts once you commit)
Mike Axiak
I agree with You. I'm DBA ... but php begginers almost always do not recognize difference between transaction and connection. RCT pattern - one Request, one Connection, one Transaction.
iddqd
A: 

Use the connection pooling, so it really doesn't matter how often you request a connection in the code. Applications wishing to be scalable should avoid rapidly creating new connections, as they potentially could have noticeable overhead for encryption setup or waiting for a authentication server.

JOTN