Jump to content

Troubles with showing a product with SQL


sykorazza

Recommended Posts

So I wanted to make an automatic table displaying all the products from the database, where you can click on its name and view the whole information of the product. The problem is that it wouldn't load the product's information.

 

Error 1: Warning: Wrong parameter count for mysqli_stmt_bind_result() in C:\wamp\www\goldenrod\index.php on line 59

Error 2: Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, object given in C:\wamp\www\goldenrod\index.php on line 61

 

 

Connection to the database:

<?php
//Database Connections
$conn = mysqli_connect('localhost', 'root', '', 'lionsierra_db') or die("Cannot find specified server");
    
//Product Database
$db_products = "SELECT * FROM `products`";
$products = mysqli_query($conn, $db_products);
?>

Here is the function:

//View product
if(isset($_GET['view_product'])) {
     //now let's build out our query
     $view_product_statement = mysqli_prepare($conn, "SELECT * FROM `products` WHERE `ID` = ?");
     mysqli_stmt_bind_param($view_product_statement, 'i', $_GET['view_product']);
     mysqli_stmt_execute($view_product_statement);
     mysqli_stmt_bind_result($view_product_statement);

     while($product=mysqli_fetch_assoc($view_product_statement)){
     //Display a product
         echo "<p><span>
             <span style='font-weight:bold;'>" . $product['name'] . "</span><br/>
             <span>$" . $product['price'] . "</span><br/>
             <span>" . $product['category'] . "</span><br/>
              <span>" . $product['description'] . "</span>
             </p>";
                    }
                    mysqli_stmt_close($view_product_statement);
                }else{
                //View all products
                echo '<table width="600" border="1" cellpadding="1" cellspacing="1">
                    <tr>
                        <th>Name</th>
                        <th>Price</th>
                        <th>Category</th>
                    </tr>';
                    while($product=mysqli_fetch_assoc($products)) {
                        echo "<tr>
                            <td><a href='./index.php?view_product=" . $product['ID'] . "'>". $product['name'] . "</a></td>
                            <td>" . $product['price'] . "</td>
                            <td>" . $product['category'] . "</td>
                        </tr>";
                        $ID = $product['ID'];
                    }
                }

Kudos!

Link to comment
Share on other sites

1 - error 1 tells you something very specific. Have you read the manual to see how the bind_result has to be built? Fix the number of args in your call - that is what it is telling you.

2 - If you had PROPERLY done error checking on your bind call, or on your query call you wouldn't have reached the fetch call which won't work since the query probably failed because of error #1

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.