/    Sign up×
Community /Pin to ProfileBookmark

closing database connection

I have an include on my pages that connects to my database, and an include at the bottom of my pages that closes this connection. I am getting an error on the server that says:

[04-Feb-2008 07:51:28] PHP Warning: mysql_close(): 5 is not a valid MySQL-Link resource in /home/goldengl/public_html/inc/closedb.php on line 2

I’m not sure why I’m getting this. Here is my open connection and closing connection code:

Open connection include

[code=php]
$dbhost = “localhost”;
$db = “goldengl_inet”;
$username = “goldengl_username”;
$password = “password”;
$connect = mysql_connect($dbhost, $username, $password) or die(‘Error connecting to mysql’);
mysql_select_db($db);
[/code]

Closing connection include

[code=php]
mysql_close($connect);
[/code]

Thanks

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 04.2008 — Do you reuse the $connect variable somewhere in the script? For debugging, you might want to put in the following just before the mysql_close():
[code=php]
echo "<pre>";
var_dump($connect);
echo "</pre>";
[/code]

In any case, my first suggestion would be to just remove the mysql_close() entirely. If you are waiting until the end of the script to close it, then you really aren't gaining anything since the connection will automatically be closed upon script termination. If you really feel a need to do it anyway, you can just leave out the $connect parameter, and it will automatically try to close the last connection opened by the script.
×

Success!

Help @jrthor2 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.1,
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,
)...