Jump to content

Warning: mysql_num_rows() error for my uni project


jamesyrawr

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

$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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.