Let’s say I call multiple functions on a given page.
function getProfile {
<mysql code to connect>
...
...
}
function getMatchingUsers {
<mysql code to connect>
...
...
}
If I call the two functions above on a given page, wouldn’t that open multiple connections to the DB? I assume php would automatically close the DB connection after the first function runs, but then it would automatically open another connection for the second function. My question is, what if my page makes a bunch of calls to the DB. Is this good for memory, io, etc…?
The simplest way is to make the connection at the beginning of your script. The mysql functions will use the last open connection to do their work, so there is no need to connect inside each function.