Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::fetch_assoc()
I am attempting to loop through the database query and display the results in rows in an html table. I have tried all kinds of different code snippets from all over the web. I have spent over 40 minutes on this and I still cannot get the results that I want.
Can someone show me how to correct my code and explain why the code was wrong?
<?php
include 'connection.php'; // includes the connection.php file to connect to the database
// query to database and prepares and executes
$query = "SELECT id, first_name, last_name FROM customers ORDER BY last_name asc";
$result = $db->prepare($query);
$result->execute();
$result->store_result();
// echo $result;
// count the number of customers
$total_customers = $result->num_rows;
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Granger Customers</title>
<link href="assets/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<!--start container to center design in browser-->
<div class="container">
<div class="row" style="margin-bottom:30px">
<div class="col-xs-12">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="customers.php">Customers</a>
</li>
<li class="nav-item">
<a class="nav-link" href="">Search</a>
</li>
<li class="nav-item">
<a class="nav-link" href="">Add New</a>
</li>
</ul>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<table class="table table-bordered table-striped table-hover">
<p><strong>There are a total of <?PHP echo $total_customers; ?> customers.</strong></p> <!-- output total number of customers -->
<thead>
<tr class="success">
<th class="text-center">#</th>
<th>Customer ID</th>
<th>Last Name</th>
<th>First name</th>
<th class="text-center">Details</th>
</tr>
</thead>
<tbody>
<!--start of row that you need to include for each item returned from the query-->
<?php
while($result = $result->fetch_assoc($query)) {
'<tr>';
echo '<td>'.$row["id"].'</td>';
echo '<td>'.$row["first_name"].'</td>';
echo '<td>'.$row["last_name"].'</td>';
'</tr>';
}
?>
<tr>
<td class="text-center active">1</td>
<td>20597</td>
<td>Aadil</td>
<td>Kareem</td>
<td class="text-center"><a class="btn btn-xs btn-primary" href="details.php?id=20597">Details</a></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!--end container to center design in browser-->
</body>
</html>
Subject
Written By
Posted
Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::fetch_assoc()
November 24, 2018 06:30PM
November 24, 2018 07:27PM
November 25, 2018 01:58PM
November 25, 2018 03:18PM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.