/    Sign up×
Community /Pin to ProfileBookmark

using PHP mysql_close()

A dumb little question.

PHP.net says regarding the use of mysql_close(): “Using mysql_close() isn’t usually necessary, as non-persistent open links are automatically closed at the end of the script’s execution.” [url]http://us3.php.net/manual/en/function.mysql-close.php[/url]

However I have noticed Resource ID numbers in the many hundreds. Will the resource number stay low if the mysql_close function is used? Is there a limit to the resource ID number?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@Phill_PaffordMar 24.2009 — I don't think there is a limit and using the mysql_close function on a persistent connection might throw a warning or maybe even an error. I would suggest to check you httpd logs for the warning(s).

are you using the mysql_pconnect()?
Copy linkTweet thisAlerts:
@NogDogMar 24.2009 — Many things in PHP use resource IDs, so the numbers you see are not necessarily all MySQL connections. For example, just running the following on my local PC apparently uses at least 5 resources, at least 2 of which are not even knowable within the script:
[code=php]
<pre><?php
$connx = mysql_connect('localhost', '&&&&&&', '######');
$curl = curl_init();
$fh = fopen('test.html', 'r');
var_dump($connx);
var_dump($curl);
var_dump($fh);
?></pre>
[/code]

Output:
<i>
</i>resource(2) of type (mysql link)
resource(3) of type (curl)
resource(5) of type (stream)
Copy linkTweet thisAlerts:
@DasherauthorMar 25.2009 — I am not using a persistent connection. I am just using mysql_connect($host,$name,$pw);

Basically am using a web page that has a form that sends data to mysql, by using <form action="<?php echo $PHP_SELF; ?>" method ="POST">

The connection is opened when the page loads, and a number of queries are executed, and when the form is sent, the page reloads and a new set of queries are sent. A new mysql_connect is executed each time page is loaded. I don't really use a mysql_close(), but I have been thinking maybe I should.

I had run some test code this morning, and I just connected to mysql from a command line, and the connection id is 402.
Copy linkTweet thisAlerts:
@NogDogMar 25.2009 — Is this on a shared host where other scripts are running? If so, it could be resources used by those other scripts. And if some of those other scripts are abusing persistent connections, there could be a lot of dead connections floating around. (A shared host I was on some time ago had an hourly cron job that would go around resetting the database or some such until they got smart and disabled the mysql_pconnect() function.)
Copy linkTweet thisAlerts:
@svidgenMar 25.2009 — Any connections made using mysql_connect() will be closed at the end of script execution, as the documentation indicates. Even if they're not, it shouldn't affect your resource ID's like this. As far as I understand it, resources start counting at 0 (or possibly 1) at the beginning of the script. And if you were getting an old (persistent) resource, why would it's ID number increment?

You can expect that some resources are consumed without being explicitly assigned to a variable. For example, the internal functioned that deal with sessions undoubtedly consume some resources. Though, getting a resource ID in the hundreds does seems a little concerning.

Perhaps you should dump all your variables into a PRE at the end of your script. Then do a real quick search for "Resource" ...
[code=html]print "<pre>" . htmlspecialchars(print_r(get_defined_vars(),1)) . "</pre>";[/code]
When I do this to my development server for svidgen.com, I find that I only have one resource listed at the time of the dump: Resource #5. And since I only establish a database connection once (that I know of) on each page, I presume the first five (or four) relate to opening included files and storing session data: one session + template include + page/function + analytics include + db function include = 5.

In regards to mysql_close(), I would actually advise against using it. PHP allegedly destroys resources on it's own that aren't used anymore. And you're more likely to accidentally close a connection that you need later on, if you do it explicitly.

Do you have a lot of includes/requires? Do your includes have includes/requires? Is there any chance you're connecting to a database repeatedly?
×

Success!

Help @Dasher 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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