php输出从mysql到html表 [英] php output from mysql to html table

查看:133
本文介绍了php输出从mysql到html表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在一个网站正常运行时间搜索引擎,但我有一个愚蠢的问题。我想在表中输出多个mysql行,但是我的代码下面导致为每个发现的行创建一个单独的表。先谢谢您的协助


currently I'm working on a site uptime search engine however I'm having a silly issue. I want to output more than one mysql row in a table however my code below causes a separate table to be created for each row thats found. Thank you in advance for your assistance

 $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;
?>


推荐答案

p>

Replace the while inside the if with this:

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

这篇关于php输出从mysql到html表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆