/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Configure Default Tear Down?

If you’re anything like me you almost never use mysql_close(), even though it is the [I]correct[/I] thing to do.

I am interested in some kind of configuration setting that will call mysql_close() (any arbitrary function really) at the end of each script.

Bonus points of you can find a setup function also ?

[FONT=”Courier New”][COLOR=”Sienna”]IMPORTANT:[/COLOR][/FONT] I already know how to deal with this through MySQL. I am looking to solve it using Apache or PHP (preferably PHP). :p

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 17.2011 — There's really no need to explicitly call mysql_close() at script close, since it automatically happens. The only reason to call it is if you want to close the connection [i]before[/i] you reach the end of the script (e.g. it's possible there will be a non-trivial amount of processing left in the script after you're done with accessing the database).

If you handle your DB connection through a class, you could add a __destruct() method to that class that closes the connection, then unset your DB object when done with it (though to be honest I'm not sure how "smart" the PHP compiler is, and if its optimization already releases/destroys objects that are no longer referenced after some point in the code).
Copy linkTweet thisAlerts:
@eval_BadCode_authorJun 18.2011 — I've never seen someone using PHP do this:

[code=php]
try { open($resource); }
catch( ... ) { ... }
finally { close($resource); }
[/code]


Operating Systems have to negotiate these resources with all of the applications running in user-land and only so many resources can be used at any given point in time.

Since almost no one using PHP seems to close them all (including myself), it makes sense for PHP to automatically close them at the end of the script ... neat :p
Copy linkTweet thisAlerts:
@NogDogJun 18.2011 — It's probably part of the nature of PHP in a web environment, where it essentially launches a new PHP process for each page request, so once the script is done, its process is done and it releases (at least should ? ) all its resources. (It's a bit foggier now since most Apache hosts run a PHP module that is always running, so "process" might not be the best word for whatever it is that is spawned for each page request, but functionally it works to think of it that way.)

The MySQL extension in PHP does provide for a persistent DB connection (e.g. mysql_[b]p[/b]connect()) that might help avoid any resource contention, but my (very limited) experience with it suggests if you don't have everything done just right (including MySQL configuration), you can easily end up shooting yourself in the foot with a bunch of zombie MySQL processes. So I usually just fire up my MySQLi object, pass it around as needed, and don't worry any more about it -- at least until the day comes when some sysadmin tells me I'm using up too much MySQL process time. ?
Copy linkTweet thisAlerts:
@eval_BadCode_authorJul 11.2011 — I wanted to revisit this thread, because I found something great, infact it's exactly what I was looking for.

There IS a setup and teardown option for PHP!

setup = http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file

teardown = http://www.php.net/manual/en/ini.core.php#ini.auto-append-file

Example of how to set it in .htaccess:
[CODE]
#####################
# _ #
# _ __ | |__ _ __ #
#| '_ | '_ | '_ #
#| |_) | | | | |_) |# values
#| .__/|_| |_| .__/ #
#|_| |_| #
#####################
php_flag display_errors On
php_value error_reporting 7

## http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file ##
[B]php_value auto_prepend_file 'bootstrap.php'[/B]
################################################


## http://httpd.apache.org/docs/2.0/mod/core.html#errordocument ##
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php

Order Deny,Allow
Allow from All

<FilesMatch ".php$">
AddHandler x-httpd-php5.3 .php
</FilesMatch>

## http://httpd.apache.org/docs/2.0/mod/core.html#options ##
Options -Indexes

<Files php.ini>
Deny from All
Satisfy All
</Files>

<Files bootstrap.php>
Deny from All
Satisfy All
</Files>

[/CODE]



bootstrap.php (the file that is now parsed before all other files):
[code=php]
<?php

function __autoload($class_name) {
require_once('./classes/class.' . $class_name . '.php');
}

DEBUG::setErrorReporting();
User::poppop(); #Access Logs

?>
[/code]



Much better! Thread marked resolved ?

Edit: Now pages executed as PHP will still call the bootstrap code even if the page itself contains no PHP at all ?
×

Success!

Help @eval_BadCode_ 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 6.15,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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