0

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>
4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by cwarn23
0

The error means you either have no items in your database, or you put the wrong table in the query.

0
SELECT * FROM products WHERE prod_id = [$id]

The brackets cause the query to fail, change it to:

SELECT * FROM products WHERE prod_id = $id
0

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>
0

It means that the 'price' column does not exist. Keep in mind php is case sensitive so make sure that it's not 'Price' or 'PRICE' or something else other than 'price'. You can always do print_r($row2) to find out what it is.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.