Question about PHP class and mysql connector

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
WilliamLou
Forum Newbie
Posts: 12
Joined: Fri Mar 14, 2008 1:32 pm

Question about PHP class and mysql connector

Post 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!!!
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: Question about PHP class and mysql connector

Post by anto91 »

You might want to add Mysql_Close in the destructor or Mysql_free_results.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Re: Question about PHP class and mysql connector

Post by Rovas »

You can use one from phpclasses.org or follow the snippet that I put on this topic.
WilliamLou
Forum Newbie
Posts: 12
Joined: Fri Mar 14, 2008 1:32 pm

Re: Question about PHP class and mysql connector

Post 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.
WilliamLou
Forum Newbie
Posts: 12
Joined: Fri Mar 14, 2008 1:32 pm

Re: Question about PHP class and mysql connector

Post 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.
Post Reply