This is a complete n00b question but I cannot figure out what is wrong. As i have a class doing pretty much the same as what is happening in the error that's working :S

Fatal error: Call to a member function num_rows() on a non-object.

class basket
{
private $items;
private $quantity;
private $database;
private $error;

public static $instance;

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'];
		//set the standard error
		$this->error = 'Product Does Not Exist!';
	}

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

public function add($id)
	{
		if ($this->items[$id] <= 0)
			{
				$this->items[$id] = 0;
			}
		if (ereg("([A-Z0-9]+)", $id))
			{
				$results = $this->database->query("SELECT * FROM `products` WHERE code='$id'");
				//line 52 - Fatal error: Call to a member function num_rows() on a non-object
				if ($results->num_rows() == 1)
					{
						$this->items[$id]++;
					}
				else
					{
						return $this->error;
					}
			}
		else
			{
				return $this->error;
			}
	}

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

//note: I didn't include all the class as it just hinders the problem

    It sounds like the database connection object was not created.

      If the DB object wasn't created surly the $this->database->query() would error first :S

      It seems that I was trying to call num_rows as a function rather than a property.\

      public function add($id)
      		{
      			if ($this->items[$id] <= 0)
      				{
      					$this->items[$id] = 0;
      				}
      			if (ereg("([A-Z0-9]+)", $id))
      				{
      					if ($results = $this->database->query("SELECT * FROM `products` WHERE code='$id'"));
      						{
      							if ($results->num_rows == 1)
      								{
      									$this->items[$id]++;
      									$results->close();
      									return 1;
      								}
      							else
      								{
      									return $this->error;
      								}
      						}
      				}
      			else
      				{
      					return $this->error;
      				}
      		}
      

      This seems to work now.

      Anyone know what causes these errors:
      Warning: Call to private basket::__destruct() from context '' during shutdown ignored in Unknown on line 0

      Warning: Call to private database::__destruct() from context '' during shutdown ignored in Unknown on line 0

        I have not seen it explicitly stated anywhere yet, but it appears that the destructor must have public visibility, thus the error since you defined your __destruct() as private.

          Sorry I should have updates this when I sorted it.

          Your right nogdog destructor's need to be public. The logic in that I cannot understand.

          mysqli seems to be pretty poorly documented in some areas as well from the research I have been doing. Some functions sound like there going to be really useful but when you look into them they have no explanation.

            Your right nogdog destructor's need to be public. The logic in that I cannot understand.

            If it is non-public, the object cannot be destroyed (unless it is protected and invoked from a derived class scope).

              Write a Reply...