jamesyrawr Posted October 29, 2010 Share Posted October 29, 2010 I seem to get this error message when I run this please can someone help? Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\unisite\products.php on line 11 I can't figure this out at all <?php if(isset($_GET['action']) && $_GET['action'] == "add"){ $id = intval($_GET['id']); if(isset($_SESSION['cart'][$id])){ $_SESSION['cart'][$id]['quantity']++; } else { $sql2 = "SELECT * FROM products WHERE prod_id = [$id] "; $query2 = mysql_query($sql2); if(mysql_num_rows($query2) != 0){ $row2 = mysql_fetch_array($query2); $_SESSION['cart'][$row2]['prod_id'] = array("quantity" =>1, "price" => $row2['price']); } else { $message = "This product id is invalid"; } } } ?> <h2 class="message"> <?php if(isset($message)){echo $message;}?> </h2> <h1>Products page</h1> <table> <tr> <th>Name</th> <th>Description</th> <th>Price</th> <th>Action</th> </tr> <?php $sql = "SELECT * FROM products ORDER BY name ASC"; $query = mysql_query($sql)or die(mysql_error()); while($row = mysql_fetch_assoc($query)){ ?> <tr> <td><?php echo $row['name']; ?> </td> <td><?php echo $row['prod_description']; ?> </td> <td>£<?php echo $row['prod_price']; ?> </td> <td><a href="index.php?page=products&action=add&id=<?php echo $row['id_prod']; ?>">add to cart</a></td> </tr> <?php /** * @author James Bell * @copyright 2010 */ } ?> </table> Link to comment https://forums.phpfreaks.com/topic/217165-warning-mysql_num_rows-error-for-my-uni-project/ Share on other sites More sharing options...
btherl Posted October 29, 2010 Share Posted October 29, 2010 Your query failed because the sql is invalid, so $query2 is the boolean value "false". A simple way to check for errors is like this: $query2 = mysql_query($sql2) or die("Query failed: $sql2\n": mysql_error()); To fix the query, try removing the [ and ] characters. Link to comment https://forums.phpfreaks.com/topic/217165-warning-mysql_num_rows-error-for-my-uni-project/#findComment-1127832 Share on other sites More sharing options...
jamesyrawr Posted October 29, 2010 Author Share Posted October 29, 2010 thanks for that i had made an error in my coding and typed one of the field names differently too how it was in my database too. but now im getting a new error in it Notice: Undefined index: price in C:\wamp\www\unisite\products.php on line 13 <?php if(isset($_GET['action']) && $_GET['action'] == "add"){ $id = intval($_GET['id']); if(isset($_SESSION['cart'][$id])){ $_SESSION['cart'][$id]['quantity']++; } else { $sql2 = "SELECT * FROM products WHERE id_prod = $id "; $query2 = mysql_query($sql2); if(mysql_num_rows($query2) != 0){ $row2 = mysql_fetch_array($query2); $_SESSION['cart'][$row2['id_prod']] = array("quantity" => 1, "price" => $row2['price']); } else { $message = "This product id is invalid"; } } } ?> <h2 class="message"> <?php if(isset($message)){echo $message;}?> </h2> <h1>Products page</h1> <table> <tr> <th>Name</th> <th>Description</th> <th>Price</th> <th>Action</th> </tr> <?php $sql = "SELECT * FROM products ORDER BY name ASC"; $query = mysql_query($sql)or die(mysql_error()); while($row = mysql_fetch_assoc($query)){ ?> <tr> <td><?php echo $row['name']; ?> </td> <td><?php echo $row['prod_description']; ?> </td> <td>£<?php echo $row['prod_price']; ?> </td> <td><a href="index.php?page=products&action=add&id=<?php echo $row['id_prod']; ?>">add to cart</a></td> </tr> <?php /** * @author James Bell * @copyright 2010 */ } ?> </table>> Link to comment https://forums.phpfreaks.com/topic/217165-warning-mysql_num_rows-error-for-my-uni-project/#findComment-1127938 Share on other sites More sharing options...
Anti-Moronic Posted October 29, 2010 Share Posted October 29, 2010 $row2['price'] doesn't exist or is empty. You can solve the problem by first ensuring the data is what you expect: echo $row2['price'] ..and then if necessary, declaring the variable: $row2['price'] = ''; //note, that must happen prior to $row2 = mysql_fetch_array($query2); Link to comment https://forums.phpfreaks.com/topic/217165-warning-mysql_num_rows-error-for-my-uni-project/#findComment-1127940 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.