I have a db class that sets up a connection. It then has methods to
query the db and fetch results etc that encapsulate the normal mysql
functions (i.e. mysql_query($sq l)).
I seem to get this error only when I try and create a new copy of this
class when one already is open.
For instance I query a db to get all the event ids from the table. I
then go through an load each event from the id. Because the query is
being issued whilst a connection is already open the resources seem to
be getting confused.
Below is my db code and the example.
class Database{
private $conn;
function __construct(){
$this->conn = mysql_connect(D B_SERVER, DB_SERVER_USERN AME,
DB_SERVER_PASSW ORD)
or die('Could not connect: ' . mysql_error());
if($this->conn){
mysql_select_db (DB_DATABASE) or die('Could not connect: ' .
mysql_error());
}
}
function query($sql){
$query = mysql_query($sq l);
if(!$query){
die("Error: in database query");
}else{
return $query;
}
}
function fetch_array($re sult){
$array = mysql_fetch_ass oc($result);
return $array;
}
function close(){
mysql_close($th is->conn);
$this->conn = null;
}
function __destruct(){
$this->close();
}
}
Example
function get_events($whe re = null){
$db = new Database();
$sql = "SELECT * FROM event ";
if(isset($where )){
$sql .= $where;
}
$query = $db->query($sql);
$res = array();
while($result = $db->fetch_array($q uery)){
$event = new Event($result['id']);
$res[] = $result;
}
return $res;
}
//Loads an event from the database using the id as primary key
function load($id){
//Get the event from the database
$db = new Database();
$sql = sprintf("SELECT * FROM event WHERE id = %u", $id);
$query = $db->query($sql);
$result = $db->fetch_array($q uery);
load is called from constructor.
If anyone has an idea please please help this problem is very annoying.