从mysql到html表的php输出

从mysql到html表的php输出,php,html,mysql,css,Php,Html,Mysql,Css,目前,我在一个网站正常运行时间搜索引擎工作,但我有一个愚蠢的问题。我想在一个表中输出多个mysql行,但是下面的代码会为找到的每一行创建一个单独的表。提前感谢您的帮助 $searchTerm = trim($_GET['searchterm']); //check whether the name parsed is empty if($searchTerm == "") { echo "Please enter something to search for..."; exit();

目前,我在一个网站正常运行时间搜索引擎工作,但我有一个愚蠢的问题。我想在一个表中输出多个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;
?>
$searchTerm=trim($\u GET['searchTerm']);
//检查解析的名称是否为空
如果($searchTerm==“”)
{
echo“请输入要搜索的内容…”;
退出();
} 
//数据库连接信息
$host=“localhost”//服务器
$db=“db NAME”//数据库名称
$user=“user”//DABASE用户名
$pwd=“密码”//密码
$link=mysqli_connect($host、$user、$pwd、$db);
$query=“从名称或des(如“%$searchTerm%”)所在的站点中选择*;
$results=mysqli_查询($link,$query);
if(mysqli_num_rows($results)>=1)
{
while($row=mysqli\u fetch\u数组($results))
{
回声';
回显“名称描述:状态”;
回声“;
echo$row['name'];
回声“;
echo$row['des'];
回声“;
echo$row['status'];
回声“;
回声“;
}
}
其他的
echo“没有与名称匹配的记录”$搜索词;
?>

在if内更换以下部件:

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';
回显“名称描述:状态”;
while($row=mysqli\u fetch\u数组($results))
{
回声“;
echo$row['name'];
回声“;
echo$row['des'];
回声“;
echo$row['status'];
回声“;
}
回声“;

第一行和最后一行代码应该在循环之外。代码应如下所示:

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>";
echo';
while($row=mysqli\u fetch\u数组($results))
{
回显“名称描述:状态”;
回声“;
echo$row['name'];
回声“;
echo$row['des'];
回声“;
echo$row['status'];
回声“;
}
回声“;

在打印所有行之间的标题时,您可以将表标记放在循环外部,只有tr在循环中。