Jump to content

mysqli::query() [function.mysqli-query]: Couldn't fetch database on destructor


knowj

Recommended Posts

I am currently getting this error within my wishlist class

 

Warning: mysqli::query() [function.mysqli-query]: Couldn't fetch database in /url/class_wishlist.php on line 34

<?php
public function __destruct()
	{
		$this->items = implode(',', $this->items);
//line 34			
$this->database->query("UPDATE `members` SET wishlist='".$this->items."' WHERE id='".$_SESSION['uid']."'");
	}
?>

 

Can you normally run SQL queries within a destruct?

 

wishlist class:

<?php
class wishlist extends basket
{
private $wishlist;
private static $instance;

private function __construct()
	{
		//connect to the database
		$this->db = database::getinstance();
		$this->items = array();
		//retrive the users wishlist
		if ($results = $this->database->query("SELECT `wishlist` FROM `members` WHERE id='".$_SESSION['uid']."'"))
			{
				//return the result into $row
				$row = $results->fetch_array(MYSQLI_ASSOC);
				//if the wishlist is populated
				if (!empty($row['wishlist']))
					{
						//explode the wishlist into an array
						$this->wishlist = explode(',', $row['wishlist']);
						//loop the exploded wishlist out into the items format
						foreach ($this->wishlist as $name => $value)
							{
								$this->items[$value] = '1';
							}
					}
			}
	}

public function getinstance()
	{
		if(self::$instance === null)
			{
				$c = __CLASS__;
				self::$instance = new $c;
			}
		return self::$instance;
	}

//prevent clone
public function __clone()
	{
		throw new Exception("Cannot clone ".__CLASS__." class");
	}

public function __destruct()
	{
		if ($this->items)
			{
				//set the wishlist to an array to ensure it is empty
				$this->wishlist = array();
				//loop through the items object to reformat the array into wishlist
				foreach ($this->items as $name => $value)
					{
						$this->wishlist[] = $name;
					}
				//implode the wishlist
				$this->wishlist = implode(',', $this->wishlist);

				//save the wishlist to the users wishlist field
				$query = "UPDATE `members` SET wishlist='".$this->wishlist."' WHERE id='".$_SESSION['uid']."'";

				$this->database->query($query);
			}
	}

}
?>

 

relevant sections of basket class

<?php
session_start();
class basket
{
protected $items;
private $quantity;
protected $database;

private static $instance = NULL;

private function __construct()
	{
		//create an instance of the database class and connect to the database
		$this->database = database::getinstance();
		//load the session items into the property $this->items
		$this->items = $_SESSION['items'];
	}

public function getinstance()
	{
		if(self::$instance === null)
			{
				$c = __CLASS__;
				self::$instance = new $c;
			}
		return self::$instance;
	}

//prevent clone
public function __clone()
	{
		throw new Exception("Cannot clone ".__CLASS__." class");
	}

/*////
//Add the item to the $basket->items property after checking is valid
*/////	
public function add($id)
	{
		//check the value isn't an invalid format
		if (!preg_match("/^[A-Z0-9]+$/", $id))
			{
				return 0;
			}

		//SELECT * FROM `products` WHERE code='$id'
		if ($results = $this->database->query("SELECT * FROM `products` WHERE id='$id'"));
			{
				//if the product is in the database
				if ($results->num_rows == 1)
					{
						//clear the results
						$results->close();
						if ($this->items[$id] < 1)
							{
								$this->items[$id] = 1;
							}
						return 1;
					}
				else
					{
						return 0;
					}
			}
	}

function remove($id)
	{
		if ($this->items[$id])
			{
				unset($this->items[$id]);
				return 1;
			}
	}

function getitems()
	{
		if ($this->items)
			{
				foreach ($this->items as $name => $value)
					{
						if (empty($where))
							{
								$where = "'$name'";
							}
						else
							{
								$where .= ", '$name'";
							}
					}
					return $this->database->query("SELECT * FROM `products` WHERE `id` IN ($where)");
			}
		return 0;
	}

public function countitems()
	{
		return count($this->items);
	}

function __destruct()
	{
		$_SESSION['items'] = $this->items;
	}
}
?>

 

product page calling the wishlist->add() function

<?php
if ($_POST['wishlist'])
{
	$wishlist = wishlist::getinstance();
	if ($wishlist->add($_POST['item']))
		{
			//header("Location: ".$_SERVER['HTTP_REFERER']);
			exit();
		}
	else
		{
			//header("Location: /error/3/");
			exit();
		}
}
?>

 

I have been looking everywhere with no joy

 

Cheers

J

That's so strange.  Do this:

 

if (is_resource($this->db)) {
echo "Connection object remains active.";
}
else {
echo "Connection destroyed.";
}

 

Put that in the destructor.  I would honestly just make a function to delete everything and call it right before you destroy the object, outside of the destructor.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.