Jump to content

Fatal error: Call to a member function query() on a non-object PLEASE HELP


damiendarien

Recommended Posts

<?php

class OnlineStore {

private $DBConnect = NULL;
private $storeID = "";
private $inventory = array();
private $shoppingCart = array();
function __construct() {
	include("inc_OnlineStoreDB.php");
	$this->DBConnect = $DBConnect;
}
function __destruct() {
	if (!$this->DBConnect->connect_error)
		$this->DBConnect->close();
}

public function setStoreID($storeID) {
	if ($this->storeID != $storeID) {
		$this->storeID = $storeID;
		$SQLstring = "SELECT * FROM inventory " .
					" where storeID = 'jw" .
					$this->storeID . "'";
		$QueryResult = $this->DBConnect->query($SQLstring);
		if ($QueryResult === FALSE) {
			$this->storeID = "";
		} else {
			$this->inventory = array();
			$this->shoppingCart = array();
			while (($Row = $QueryResult->fetch_assoc()) !== NULL) {
				$this->inventory[$Row['productID']] = array();
				$this->inventory[$Row['productID']]['name'] = $Row['name'];
				$this->inventory[$Row['productID']]['description'] = $Row['description'];
				$this->inventory[$Row['productID']]['price'] = $Row['price'];
				$this->shoppingCart[$Row['productID']] = 0;
			}
		}
	}
}

public function getStoreInformation() {
	$retval = FALSE;
	if ($this->storeID != "") {
		$SQLstring = "SELECT * FROM store_info " .
					" WHERE storeID = '" .
					$this->storeID . "'";
		$QueryResult = @$this->DBConnect->query($SQLstring);
		if ($QueryResult !== FALSE) {
			$retval = $QueryResult->fetch_assoc();
		}
	}
	return($retval);
}

public function getProductList() {
	$retval = FALSE;
	$subtotal = 0;
	if (count($this->inventory) > 0) {
		echo "<table width='100%'>\n";
		echo "<tr><th>Product</th><th>Description</th>" .
				"<th>Price Each</th><th># in Cart</th>" .
				"<th>Total Price</th><th> </th></tr>\n";
		foreach ($this->inventory as $ID => $Info) {
			echo "<tr><td>" .
					htmlentities($Info['name']) . "</td>\n";
			echo "<td>" . htmlentities($Info['description']) . "</td>\n";
			printf("<td class='currency'>$%.2f</td>\n", $Info['price']);
			echo "<td class='currency'>" . $this->shoppingCart[$ID] . "</td>\n";
			printf("<td class='currency'>$%.2f</td>\n", $Info['price'] * $this->shoppingCart[$ID]);
			echo "<td><a href='" . $_SERVER['SCRIPT_NAME'] . "?PHPSESSID=" . session_id() . "&ItemToAdd=$ID'>Add " . " Item</a></td>\n";
			$subtotal += ($Info['price'] * $this->shoppingCart[$ID]);
		}
		echo "<tr><td colspan='4'>Subtotal</td>\n";
		printf("<td class='currency'>$%.2f</td>\n", $subtotal);
		echo "<td>$nbsp;</td></tr>\n";
		echo "</table>";
		$retval = TRUE;
	}
	return($retval);
}

public function addItem() {
	$ProdID = $_GET['ItemToAdd'];
	if (array_key_exists($ProdID, $this->shoppingCart))
		$this->shoppingCart[$ProdID] += 1;
}


}

?>

 

having an issue to where nothing is appearing on the page and getting the

Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\PHP\Chapter.10\class_OnlineStore.php on line 24

 

HERE IS THE MAIN PAGE

<?php
session_start();
require_once("class_OnlineStore.php");

$storeID = "COFFEE";
$storeInfo = array();

if (class_exists("OnlineStore")) {
if (isset($_SESSION['currentStore']))
	$Store = unserialize($_SESSION['currentStore']);
else {
	$Store = new OnlineStore();
}
$Store->setStoreID($storeID);
$storeInfo = $Store->getStoreInformation();
} else {
$ErrorMsgs[] = "The OnlineStore class is not available!";
$Store = NULL;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $storeInfo['name']; ?></title>
<link rel="stylesheet" type="text/css" href="<?php echo $storeInfo['css_file']; ?>" />
</head>

<body>
<h1><?php echo htmlentities($storeInfo['name']); ?></h1>
<h2><?php echo htmlentities($storeInfo['description']); ?></h2>
<p><?php echo htmlentities($storeInfo['welcome']); ?></p>

<?php 
$Store->getProductList();
$_SESSION['currentStore'] = serialize($Store);
?>	

</body>
</html>

 

I HAVE REVIEWED IT OVER AND OVER AGAIN AND I CANT SEE THE PROBLEM. MAYBE SOMEONE FROM THE OUTSIDE CAN HELP OUT.

Link to comment
Share on other sites

  • 4 years later...
  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.