Hii i m trying to make a shopping cart and it showing 2 errors: 1.Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\e-commerce\php_cart\cart.php on line 44 2.Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\e-commerce\php_cart\cart.php on line 154 Here is source code: Can anybody help me to debug it <?php include("db.php"); switch($_GET["action"]) { case "add_item": { AddItem($_GET["id"], $_GET["qty"]); ShowCart(); break; } case "update_item": { UpdateItem($_GET["id"], $_GET["qty"]); ShowCart(); break; } case "remove_item": { RemoveItem($_GET["id"]); ShowCart(); break; } default: { ShowCart(); } } function AddItem($itemId, $qty) { // Will check whether or not this item // already exists in the cart table. // If it does, the UpdateItem function // will be called instead global $dbServer, $dbUser, $dbPass, $dbName; $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName); // Get a connection to the database // Check if this item already exists in the users cart table $result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId"); $row = mysql_fetch_row($result); $numRows = $row[0]; if($numRows == 0) { // This item doesn't exist in the users cart, // we will add it with an insert query @mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)"); } else { // This item already exists in the users cart, // we will update it instead UpdateItem($itemId, $qty); } } function UpdateItem($itemId, $qty) { // Updates the quantity of an item in the users cart. // If the qutnaity is zero, then RemoveItem will be // called instead global $dbServer, $dbUser, $dbPass, $dbName; // Get a connection to the database $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName); if($qty == 0) { // Remove the item from the users cart RemoveItem($itemId); } else { mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId"); } } function RemoveItem($itemId) { // Uses an SQL delete statement to remove an item from // the users cart global $dbServer, $dbUser, $dbPass, $dbName; // Get a connection to the database $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName); mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId"); } function ShowCart() { // Gets each item from the cart table and display them in // a tabulated format, as well as a final total for the cart global $dbServer, $dbUser, $dbPass, $dbName; // Get a connection to the database $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName); $totalCost = 0; $result = mysql_query("select * from cart inner join pricedescription on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc"); ?> <html> <head> <title> Your Shopping Cart </title> <script language="JavaScript"> function UpdateQty(item) { itemId = item.name; newQty = item.options[item.selectedIndex].text; document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty; } </script> </head> <body bgcolor="#ffffff"> <h1>Your Shopping Cart</h1> <form name="frmCart" method="get"> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tr> <td width="15%" height="25" bgcolor="red"> <font face="verdana" size="1" color="white"> <b>Qty</b> </font> </td> <td width="55%" height="25" bgcolor="red"> <font face="verdana" size="1" color="white"> <b>Product</b> </font> </td> <td width="20%" height="25" bgcolor="red"> <font face="verdana" size="1" color="white"> <b>Price Each</b> </font> </td> <td width="10%" height="25" bgcolor="red"> <font face="verdana" size="1" color="white"> <b>Remove?</b> </font> </td> </tr> <?php while($row = mysql_fetch_array($result)) { $totalCost += ($row["qty"] * $row["itemPrice"]); // Increment the total cost of all items ?> <tr> <td width="15%" height="25"> <font face="verdana" size="1" color="black"> <select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)"> <?php for($i = 1; $i <= 20; $i++) { echo "<option "; if($row["qty"] == $i) { echo " SELECTED "; } echo ">" . $i . "</option>"; } ?> </select> </font> </td> <td width="55%" height="25"> <font face="verdana" size="1" color="black"> <?php echo $row["itemName"]; ?> </font> </td> <td width="20%" height="25"> <font face="verdana" size="1" color="black"> $<?php echo number_format($row["itemPrice"], 2, ".", ","); ?> </font> </td> <td width="10%" height="25"> <font face="verdana" size="1" color="black"> <a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a> </font> </td> </tr> <?php } // Display the total ?> <tr> <td width="100%" colspan="4"> <hr size="1" color="red" NOSHADE> </td> </tr> <tr> <td width="70%" colspan="2"> <font face="verdana" size="1" color="black"> <a href="products.php"><< Keep Shopping</a> </font> </td> <td width="30%" colspan="2"> <font face="verdana" size="2" color="black"> <b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b> </font> </td> </tr> </table> </form> </body> </html> <?php } ?>
I believe that your problem lies within this line: $result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId"); maybe the select count(*) <---? It seems that mysql is telling you that $result is invalid, it doesn't know what to look for ... I would check that syntax ... I maybe wrong though, but both seem to be calling $result
Just do - echo "select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId"; then we can see if it's a valid mysql query.
Dare I say that if you stopped hiding all of the errors and warnings by using the @ sign before those DB calls, you may actually be getting more useful messages?
1. Do not COUNT all. That is just nuts. Just count the primary key or jsut pick on column to count. 2. Do not hide errors. Properly coded php will not show errors even if the query goes bad. Hiding errors means you cannot properly debug and you end up with crappy software. instead of @, add 'or die(mysql_error());' at the end so that you can truly debug. Maybe setup logging instead. 3. instead of.... $result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId"); $row = mysql_fetch_row($result); PHP: TRY: $result = mysql_query('SELECT COUNT(cookieId) FROM `cart` WHERE `cookieId` = "'.GetCartId().'" && `itemId = "'.$itemId.'"') or die(mysql_error()); $result = mysql_result($result,0); if ($result == 0 || !isset($result) || empty($result)) { $numRows = 0; } else { $numRows = $result; } PHP: OR if you need to get data and count: $result = mysql_query('SELECT * FROM `cart` WHERE `cookieId` = "'.GetCartId().'" && `itemId = "'.$itemId.'"') or die(mysql_error()); $numrows = mysql_num_rows($result); if ($numrows >= 1) { while($items = mysql_fetch_array($result)) { $YOUR_VAR = stripslashes($items['YOUR_VAR']); $YOUR_VAR2 = stripslashes($items['YOUR_VAR2']); } } else { die('No data found.'); } PHP: That will make sure an error is not displaying unless there is an explicit mysql_error(), in which case you use the message to find out where your query went wrong and fix it.
I know this is a old topic, but I had the same problem and now this scripts works fine: products.php <?php // This page will list all of the items // from the items table. Each item will have // a link to add it to the cart include("db.php"); // Get a connection to the database $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName); $result = mysql_query("select * from items order by itemName asc"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <table cellspacing="0"> <tr> <td bgcolor="#FF0000"><font color="#FFFFFF">Product</font></td> <td bgcolor="#FF0000"><font color="#FFFFFF">Price</font></td> <td bgcolor="#FF0000"><font color="#FFFFFF">Description</font></td> <td bgcolor="#FF0000"><font color="#FFFFFF">Add</font></td> </tr> <?php while($row = mysql_fetch_array($result)) { ?> <tr> <td width="30%" height="25"> <font face="verdana" size="1" color="black"> <?php echo $row["itemName"]; ?> </font> </td> <td width="10%" height="25"> <font face="verdana" size="1" color="black"> $<?php echo $row["itemPrice"]; ?> </font> </td> <td width="50%" height="25"> <font face="verdana" size="1" color="black"> <?php echo $row["itemDesc"]; ?> </font> </td> <td width="10%" height="25"> <font face="verdana" size="1" color="black"> <a href="cart.php?action=add_item&id=<?php echo $row["itemId"]; ?>&qty=1">Add Item</a> </font> </td> </tr> <tr> <td width="100%" colspan="4"> <hr size="1" color="red" NOSHADE> </td> </tr> <?php } ?> <tr> <td width="100%" colspan="4"> <font face="verdana" size="1" color="black"> <a href="cart.php">Your Shopping Cart >></a> </font> </td> </tr> </table> </body> </html> PHP: cart.php: <?php ob_start(); //include("db.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <script language="JavaScript"> function UpdateQty(item) { itemId = item.name; newQty = item.options[item.selectedIndex].text; document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty; } </script> <title>Untitled Document</title> </head> <body> <table cellspacing="0" bgcolor="#FF0000"> <tr> <td><font color="#FFFFFF">Qty</font></td> <td><font color="#FFFFFF">Product</font></td> <td><font color="#FFFFFF">Price Each</font></td> <td><font color="#FFFFFF">Remove?</font></td> <?php $host = "localhost"; $user = "test"; $pass = "test"; $dbname = "cart"; $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>"); mysql_select_db($dbname); function GetCartId() { if(isset($_COOKIE["cartId"])) { return $_COOKIE["cartId"]; } else { session_start(); setcookie("cartId", session_id(), time()+((3600*24)*30)); } } switch($_GET["action"]) { case "add_item": { AddItem($_GET["id"], $_GET["qty"]); ShowCart(); break; } case "update_item": { UpdateItem($_GET["id"], $_GET["qty"]); ShowCart(); break; } case "remove_item": { RemoveItem($_GET["id"]); ShowCart(); break; } default: { ShowCart(); } } function ShowCart(){ $suma=mysql_query("select sum(qty) from cart"); $row1 = mysql_fetch_array($suma); $result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc"); while($row = mysql_fetch_array($result)) { // Increment the total cost of all items $totalCost += ($row["qty"] * $row["itemPrice"]); ?> <tr> <td width="15%" height="25"> <font face="verdana" size="1" color="black"> <select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)"> <?php for($i = 1; $i <= 20; $i++) { echo "<option "; if($row["qty"] == $i) { echo " SELECTED "; } echo ">" . $i . "</option>"; } ?> </select> </font> </td> <td width="55%" height="25"> <font face="verdana" size="1" color="black"> <?php echo $row["itemName"]; ?> </font> </td> <td width="20%" height="25"> <font face="verdana" size="1" color="black"> $<?php echo number_format($row["itemPrice"], 2, ".", ","); ?> </font> </td> <td width="10%" height="25"> <font face="verdana" size="1" color="black"> <a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a> </font> </td> </tr> <?php }?> <tr> <td width="100%" colspan="4"> <hr size="1" color="red" NOSHADE> </td> </tr> <tr> <td width="70%" colspan="2"> <font face="verdana" size="1" color="black"> <a href="products.php"><< Keep Shopping</a> </font> </td> <td width="30%" colspan="2"> <font face="verdana" size="2" color="black"> <b>Total: $<?php echo number_format($totalCost); echo $row["qty"];?> Ilosc: <?php echo $row1["sum(qty)"];?></b> </font> </td> </tr> </table><?php } ?> <?php function AddItem($itemId, $qty) { $result=mysql_query("select count(*) from cart where cookieId='".GetCartId()."'and itemId=$itemId"); $row=mysql_fetch_row($result); $numRows=$row[0]; if($numRows==0) { //Produkt nie znajduje sie w koszyku wiec, //mozna go dodac @mysql_query("insert into cart(cookieId, itemId, qty) values('".GetCartId()."', $itemId, $qty)"); } else { //Produkt jest juz w koszyku, zwiekszy sie ilosc UpdateItem($itemId, $qty); } } function UpdateItem($itemId, $qty) { mysql_query("update cart set qty=$qty where cookieId='".GetCartId()."' and itemId = $itemId"); } function RemoveItem($itemId) { mysql_query("delete from cart where cookieId = '".GetCartId(). "' and itemId=$itemId"); } ?> </body> </html> <?php ob_flush(); ?> PHP: Regards
Hi I know this is a very old thread. I have replied that for the all future visitors with a view to helping them: see below: $result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId"); Cart is table name here. I know that when table name is too short and without underscore thn mql shows the error. because mysql can not detect short name. when table name is too short and without underscore thn you must have to add ` both side of the table name in query. Or if u dont want to add ` thn select a name with a underscore or select a long name. See example and try below one and both will work : $result = mysql_query("select count(*) from `cart` where cookieId = '" . GetCartId() . "' and itemId = $itemId"); or, $result = mysql_query("select count(*) from cart_1 where cookieId = '" . GetCartId() . "' and itemId = $itemId");