gpt4 book ai didi

从mysql到html表的php输出

转载 作者:可可西里 更新时间:2023-11-01 07:22:48 26 4
gpt4 key购买 nike

目前我正在开发网站正常运行时间搜索引擎,但是我遇到了一个愚蠢的问题。我想在一个表中输出多个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com