Hybreeder Hybreeder - 2 months ago 9
PHP Question

Column name field showing multiple time in php?

I am searching records and records are displaying from database but column name is repeating.Please check below images.

<table border="1" align="center">
<thead>
<tr>
<th>User id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM newrecords_1 WHERE CONCAT( First_name, ' ',Last_name ) LIKE '%$name%' ORDER BY `First_name` ASC";
$result = $conn->query($query);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {?>

<tr>
<td><?php echo $row['ID'];?></td>
<td><?php echo $row['First_name'];?></td>
<td><?php echo $row['Last_name'];?></td>
</tr>
</tbody>
</table>
<?php

}
} else {
echo "0 results";
}


What i am getting

enter image description here

What i need

enter image description here

after added html code on header getting output
enter image description here

Answer

Put your table code outside of the loop like below :-

<table border="1" align="center">
    <thead>
      <tr>
      <th>User id</th>
      <th>First Name</th>
      <th>Last Name</th>
      </tr>
    </thead>
    <tbody> 
<?php
$query = "SELECT * FROM newrecords_1 WHERE CONCAT( First_name, ' ',Last_name ) LIKE '%$name%'  ORDER BY `First_name` ASC"; 
$result = $conn->query($query);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {?>
    <tr>
     <td><?php echo $row['ID'];?></td>
     <td><?php echo $row['First_name'];?></td>
      <td><?php echo $row['Last_name'];?></td>
     </tr>
    <?php } } else { echo "0 results";}?>
  </tbody>
</table>