MySQL Forums
Forum List  »  PHP

Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::fetch_assoc()
Posted by: Sean Van Zant
Date: November 24, 2018 06:30PM

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?


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);
// echo $result;

// count the number of customers
$total_customers = $result->num_rows;


<!doctype html>
<meta charset="utf-8">
<title>Granger Customers</title>
<link href="assets/bootstrap.min.css" rel="stylesheet" type="text/css">


<!--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 class="nav-item">
<a class="nav-link" href="customers.php">Customers</a>
<li class="nav-item">
<a class="nav-link" href="">Search</a>
<li class="nav-item">
<a class="nav-link" href="">Add New</a>

<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 -->
<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>

<!--start of row that you need to include for each item returned from the query-->


while($result = $result->fetch_assoc($query)) {
echo '<td>'.$row["id"].'</td>';
echo '<td>'.$row["first_name"].'</td>';
echo '<td>'.$row["last_name"].'</td>';



<td class="text-center active">1</td>
<td class="text-center"><a class="btn btn-xs btn-primary" href="details.php?id=20597">Details</a></td>



<!--end container to center design in browser-->



Options: ReplyQuote

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.