This is my database table:
I want to display this table (5 columns) on my page when clicking a button (List Users).
But I'm getting the following as output:
My code is:
<?php
$db = "*"; //masked for security
$host = "*"; //masked for security
$user = "*";//masked for security
$pwd = "*; //masked for security
$con = mysqli_connect($host,$user,$pwd,$db);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$sql="SELECT login_id,user_name,password,user_role,status_id FROM login";
$select = mysqli_query($con,$sql);
$num_rows = mysqli_num_rows($select);
echo "Number of rows : ";
echo $num_rows;
$row = mysqli_fetch_array($select, MYSQLI_ASSOC);
echo "<table>
<tr>
<th>Login ID</th>
<th>User name</th>
<th>Password</th>
<th>User Role</th>
<th>Status ID</th>
</tr>";
foreach ($row as $rows)
{
echo "<tr>";
echo "<td>" . $rows['login_id'] . "</td>";
echo "<td>" . $rows['user_name'] . "</td>";
echo "<td>" . $rows['password'] . "</td>";
echo "<td>" . $rows['user_role'] . "</td>";
echo "<td>" . $rows['status_id'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Please help me find the error in this code.


foreach ($row as $rows)instead offoreach($rows as $row). But seriously, do what @RahulMeshram says so we can see what we are working with.foreachloop there if you getting only one row withmysqli_fetch_array? You should usewhile($row= mysqli_fetch_array($select, MYSQLI_ASSOC) { ... }