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>

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

SELECT * FROM products WHERE prod_id = [$id]

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

SELECT * FROM products WHERE prod_id = $id

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>

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.