Dysan Posted November 18, 2007 Share Posted November 18, 2007 Why do I get the following error message(s), when executing the following code:? Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\Documents and Settings\Dale Piper\Desktop\Xampp\htdocs\cart.php on line 46 <?php session_start(); $con = mysql_connect("localhost","ODBC",""); if (!$con) { die(mysql_error()); } function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p>You have no items in your shopping cart</p>'; } else { // Parse the cart session variable $items = explode(',',$cart); $s = (count($items) > 1) ? 's':''; return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>'; } } $cart = $_SESSION['cart']; if ($cart) { $cart .= ','.$_GET['id']; } else { $cart = $_GET['id']; } $_SESSION['cart'] = $cart; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } } $total = 0; echo '<table>'; foreach ($contents as $id=>$qty) { $result = mysql_query("SELECT * FROM books WHERE id = '$cart'"); [b]$row = mysql_fetch_assoc($result);[/b] echo $row['title']; echo $row['price']; echo '<tr>'; echo '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; echo '<td>'.$author.' by '.$author.'</td>'; echo '<td>£'.$price.'</td>'; echo '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; echo '<td>£'.($price * $qty).'</td>'; $total += $price * $qty; echo '</tr>'; } echo '</table>'; echo '<p>Grand total: £'.$total.'</p>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/77808-problem-wth-code/ Share on other sites More sharing options...
~n[EO]n~ Posted November 18, 2007 Share Posted November 18, 2007 change this line and see what error you get $result = mysql_query("SELECT * FROM books WHERE id = '$cart'") or die("Your have an error because:<br />" . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/77808-problem-wth-code/#findComment-393809 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.