从mysql到html表的php输出

标签 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();
} 

//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>";    

关于从mysql到html表的php输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18172712/

相关文章:

javascript - 在 php 中加密数据,在 javascript 中解密

php - 在 smarty 中打破/分解字符串

html - 不需要的多个背景之前/之后的 CSS

mysql - groovy MySQLSyntaxErrorException 与 CREATE DATABASE IF NOT EXISTS

php - Zend Forms 电子邮件元素更改为文本

html - 如何在不更改 URL 栏的情况下添加 HTML 哈希链接...?

php - 从具有不同大小图像的 css 图像 Sprite 中检索图像

javascript - 创建多系列折线图,从数据库获取数据

mysql - 日期间隔错误

php - Ionic/Angular $http.get 返回预检响应具有无效的 HTTP 状态代码 404