Page 1 of 1

Connection to DB appears to die after multiple operations

Posted: Tue Feb 14, 2006 5:49 pm
by ryos
Hello there,

I have written a class (called DBMonster) to abstract various database-related operations, and a class (called TeacherInfo) to hold information from the database that also makes use of the database class to retrieve that information.

Here is the relevant code from the database class:

Code: Select all

class DBMonster  {
	var $dblink;
	var $connectedStatus;
	
	
	//----------------------------
	//Contructer; mostly harmless.
	//----------------------------
	function DBMonster ()  {
		$this->connectedStatus = false;
	}
	
	
	//-------------------------------------------------------------------
	// function connect
	// Causes the object to establish a connection to the mysql database
	//-------------------------------------------------------------------
	function connect ()  {
		$this->dblink = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
		@mysql_select_db (DB_NAME, $this->dblink);
		
		if ($this->dblink)  {
			$this->connectedStatus = true;
			return true;
		}
		else  {
			die ('Connection to database failed: ' . mysql_error());
		}

	}
	
	
	//--------------------------------------------------
	// function disconnect
	// Kills the DB connection.
	//--------------------------------------------------
	function disconnect ()  {
		@mysql_close ($this->dblink);
		$this->connectedStatus = false;
		
		if (!$this->dblink)  {
			return true;
		}
		else  {
			return false;
		}
	}
	
	
	//------------------------------------------------------------------------------
	// function query
	// Runs an arbitrary mysql query, passed in as a string, and returns the result.
	//------------------------------------------------------------------------------
	function query ($qs)  {
		if (!$this->connectedStatus)  {
			$this->connect();
		}
		
		return mysql_query ($qs, $this->dblink);
	}
And, here's the code that's causing trouble:

Code: Select all

$db = new DBMonster();
$db->connect();

$teacher = new TeacherInfo ($id);

$db->query ("Any query at all...");  //fails with an error
$db->disconnect();
The above code fails with a "Warning: mysql_query(): 7 is not a valid MySQL-Link resource in..." error. The line that produces the error is "$db->query ("anything at all");".

Passing an id to a new TeacherInfo object causes the constructor to instantiate its own DBMonster object and use it to obtain the information from the database that corresponds to the given id. It just performs several select queries from various tables using DBMonster->query, and makes several mysql_fetch_array calls to retrieve the information and initialize the object's instance variables.

If I change the problem code to this:

Code: Select all

$teacher = new TeacherInfo ($id);

$db = new DBMonster();
$db->connect();

$db->query ("Any query at all...");
$db->disconnect();
...then the error goes away!

Functionally, there is no difference between the two versions (with the exception that the second version works, and the first doesn't). Why then am I concerned? Well...I'm not terribly experienced with databases, and when I see those errors from stuff that I think ought to work but doesn't, it makes me worry that I'm doing something fundamentally wrong.

Does anyone know what might be going on here? I don't think a timeout could be happening, unless the timeout is set to a fraction of a second; the script in question executes with no perceptible delay.

Some details about my system:
php -v: PHP 4.3.11
MySQL version: 5.0.18-standard
Apache version: Apache/1.3.33
Serving to localhost...
Running on Mac OS X 10.4.4.

Thanks in advance!