php从mysql输出到html表


php output from mysql to html table

目前我正在开发一个网站正常运行时间搜索引擎,但我遇到了一个愚蠢的问题。我想在一个表中输出多个mysql行,但是下面的代码会为找到的每一行创建一个单独的表。提前感谢您的帮助

 $searchTerm = trim($_GET['searchterm']);
 //check whether the name parsed is empty
 if($searchTerm == "")
{
echo "Please enter something to search for...";
exit();
} 
//database connection info
$host = "localhost"; //server
$db = "DB NAME"; //database name
$user = "USER"; //dabases user name
$pwd = "PASSWORD"; //password

$link = mysqli_connect($host, $user, $pwd, $db);

 $query = "SELECT * FROM sites WHERE name OR des LIKE '%$searchTerm%'";
 $results = mysqli_query($link, $query);

 if(mysqli_num_rows($results) >= 1)
 {
while($row = mysqli_fetch_array($results))
{
echo '<table class="table table-striped table-bordered table-hover">'; 
echo"<TR><TD>Name</TD><TD>Description:</TD><TD>Status</TD></TR>"; 
echo "<tr><td>"; 
echo $row['name'];
echo "</td><td>";   
echo $row['des'];
echo "</td><td>";    
echo $row['status'];
echo "</TD></tr>";  
echo "</table>";    
}
    }
    else
echo "There was no matching record for the name " . $searchTerm;
?>

将if内部的while替换为:

echo '<table class="table table-striped table-bordered table-hover">'; 
echo "<tr><th>Name</th><th>Description:</th><th>Status</th></tr>"; 
while($row = mysqli_fetch_array($results))
{
  echo "<tr><td>"; 
  echo $row['name'];
  echo "</td><td>";   
  echo $row['des'];
  echo "</td><td>";    
  echo $row['status'];
  echo "</td></tr>";  
}
echo "</table>";    

第一行和最后一行代码应该在循环之外。代码应该是这样的:

echo '<table class="table table-striped table-bordered table-hover">'; 
while($row = mysqli_fetch_array($results))
{
  echo"<TR><TD>Name</TD><TD>Description:</TD><TD>Status</TD></TR>"; 
  echo "<tr><td>"; 
  echo $row['name'];
  echo "</td><td>";   
  echo $row['des'];
  echo "</td><td>";    
  echo $row['status'];
  echo "</TD></tr>";  
}
echo "</table>";