/    Sign up×
Community /Pin to ProfileBookmark

mysql_close()

If I have some classes that represent objects in a database, is it better to have a call to mysql_connect() and mysql_select_db() and then mysql_close() in only the functions that require access to the database, or is it better to just have one call to mysql_connect() and mysql_select_db() in a configuration script and require_once() that script at the top of every class, keeping the connection open throughout every script until the page is finished?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 08.2008 — For the script's efficiency, it would be better to just have one connect -> select -> close sequence. If the script is very large and complex and takes some time (in computer terms) between database operations, it might be better from the overall system/site viewpoint to have separate connections for each portion of the script that needs to access the database so as to avoid possibly having concurrent scripts have to wait for a connection. But as this would likely only be an issue for very "busy" scripts on a very busy web site, I'd say that the vast majority of the time a single connection would be my preference.
Copy linkTweet thisAlerts:
@CarthauthorSep 08.2008 — Thanks, that's the way I do it, but wanted to check it was the best.
×

Success!

Help @Carth spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...