Page 1 of 1

Question about PHP class and mysql connector

Posted: Thu Apr 03, 2008 1:12 pm
by WilliamLou
Hi everyone:
I have a question about PHP class, for example: I want to create a class in PHP for a mysql connector.
The class definition is like:
Class Database
{
var $dbh = null;
function Database ($dbc) //constructor
{
$this->dbh = mysql_connect($dbc['db_host'], $dbc['db_user'], $dbc['db_pass'], TRUE);
}
.....and several member functions to operate on those DB tables
}

Now, because I need to run the program as a backend daemon, the way I achieve this is through a infinite loop:
while (true)
{
$db_delegation = new Database ($db_settings);
.....processing on $db_delegation
sleep($time);
}

This works fine when I test the program, however I noticed some problems:

1. The mysql connector resource id (class instance $db_delegation->dbh) keeps increasing. If I run the backend daemon for a whole day, the resource id increased to 15399 and it still works fine (because in the php configure file, the #limit of mysql connector is unlimited). I'm not sure if php could free the mysql connector automatically. According to the manual, php will free the resource whenever the script ends, however my script is a backend daemon so that it will never ends.

2. Actually, I could close mysql connector explicitly in my php script (just call mysql_close ($db_delegation->dbh) as the last processing in while loop, right?) However, it's not like OO style. In that case, I want to put the mysql_close in the class's destructor and then destroy that class at the end of while loop. Then, the question is how to destroy the class in php? Just call unset ($db_delegation) and then php will call its destructor automatically, right?


Anyone could help to clarify this problem? Thanks a lot!!!

Re: Question about PHP class and mysql connector

Posted: Thu Apr 03, 2008 4:53 pm
by anto91
You might want to add Mysql_Close in the destructor or Mysql_free_results.

Re: Question about PHP class and mysql connector

Posted: Fri Apr 04, 2008 2:27 am
by Rovas
You can use one from phpclasses.org or follow the snippet that I put on this topic.

Re: Question about PHP class and mysql connector

Posted: Fri Apr 04, 2008 4:47 pm
by WilliamLou
hi anto91:
Actually, I don't have a destructor for my class yet. As I said, the script is a backend daemon, so that it won't ends itself. In that case, I think php won't call the class's destructor. Then the question is how I can call the class's destructor explicitly? Just unset the instance of my class? Thanks for your reply though.

anto91 wrote:You might want to add Mysql_Close in the destructor or Mysql_free_results.

Re: Question about PHP class and mysql connector

Posted: Fri Apr 04, 2008 4:56 pm
by WilliamLou
Except migration to mysqli, any other methods? Actually, I think explicitly calling mysql_free_results ($linkid) and mysql_close ($linkid) as the last processing step inside the while loop should solve my concern. My question just is whether it's necessary. Since the script works perfectly for almost 5 days without calling these functions (however, the mysql resource id did keep increasing). I'm running this script under CentOS 5, when I netstat the port 3306, the number of connections between Apache and MySQL seems to be fixed, so I think those resource (mysql connector, results etc..) should be cleaned somehow. Otherwise, it should be out of memory very soon.

Rovas wrote:You can use one from phpclasses.org or follow the snippet that I put on this topic.