/    Sign up×
Community /Pin to ProfileBookmark

mysql connection close issue

Hello Mates,
I think I am not being able to close mysql connection properly from php. Here is what I am doing:

mysql_connect($connection);

function x()
{
global $connection;
mysql_select_db($db_name,$connection);
…code…code…code…
mysql_close($connection);
}
function y()
{
global $connection;
mysql_select_db($db_name,$connection);
…code…code…code…
mysql_close($connection);
}

Is my approach above ok? Or am I doing crap?

Any suggestion would be great help. Thank you.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 14.2008 — I would recommend passing parameters to your functions rather than using global variables (which result in tightly coupled code).
[code=php]
function x($connection, $db_name)
{
mysql_select_db($db_name, $connection);
// ...etc....
}
[/code]

Also, depending on what you are doing, you may not want to do the mysql_close() within those functions, as it means that once you call one of those functions, the connection will be closed, so you'll have to do another connect before calling either of them again.
×

Success!

Help @tkm 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.2,
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,
)...