Link to home
Create AccountLog in
Avatar of Mark Steggles
Mark StegglesFlag for United States of America

asked on

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

Hey guys,

I'm trying to learn php and making my first cart at the moment. Found a tutorial online and have been able to save the cart contents into a session but I'm stuck on how to connect to the database to match the cart with the products in the database table.

this line

$result = $con->query($sql);

is causing the error...

Any help appreciated

<?php
		
		// login details for database
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';

// connect to database
$con = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');

// select products database
//mysql_select_db("futurek0_products", $con);

		$total = 0;
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM products WHERE ID = '.$id;
$result = $con->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>&pound;'.($PRODUCT_PRICE * $qty).'</td>';
$total += $PRODUCT_PRICE * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: &pound;'.$total.'</p>';

?>

Open in new window

SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Mark Steggles

ASKER

Thanks marqusG,

I reworked the code (see attached) from your advice and am now getting results. The tutorial I was using is http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart

--

Thanks Ray, that code looks helpful.

--

One problem is that I'm only getting one result back when I loop through the cart. Something wrong with

foreach ($contents as $id=>$qty) {  ?



Thanks
<?php
		
$total = 0;
echo '<table id="cart" cellspacing="0" cellpadding="0">
			<tr>
				<th>Product</th><th>Quantity</th><th>Price</th><th>Total</th><th>Remove</th>
			</tr>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM products WHERE ID = '.$id;
$result = mysql_query($sql,$con);
$row = mysql_fetch_array($result);
extract($row);
echo '<tr>';
echo '<td>' . $row['PRODUCT_DESC'] . '</td>';
echo '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
echo '<td>$' . $row['PRICE'] . '</td>';
echo '<td>$' . ($row["PRICE"] * $qty) . '</td>';
echo '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$total += $row['PRICE'] * $qty;
echo '</tr>';
}
echo '</table>';
echo '<p>Grand total: $'.$total.'</p>';

?>

Open in new window

Try write

$sql = 'SELECT * FROM products WHERE ID = '.$id; //why dot? it misses a '

$sql = 'SELECT * FROM products WHERE ID = '$id';

Then use this

while ($row = mysql_fetch_array($result){
  echo '<tr>';
  echo '<td>' . $row['PRODUCT_DESC'] . '</td>';
  echo '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
  echo '<td>$' . $row['PRICE'] . '</td>';
  echo '<td>$' . ($row["PRICE"] * $qty) . '</td>';
  echo '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
  $total += $row['PRICE'] * $qty;
  echo '</tr>';
}

I stronglky recommend you to read this tutorial and related book: http://articles.sitepoint.com/article/php-mysql-tutorial.

Good luck
thanks guys