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!