gpt4 book ai didi

php - 如何从表中输出必要的行?

转载 作者:行者123 更新时间:2023-11-30 21:25:43 25 4
gpt4 key购买 nike

我正在编写一个包含表格的简单页面。需要显示表格的前五行。但由于某种原因,显示了表格的最后五行。如何显示表格的前五行?

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$host = 'localhost';
$database = 'books';
$user = 'root';
$password = 'root';
$link = mysqli_connect($host, $user, $password, $database)
or die("Error " . mysqli_error($link));
$query ="SELECT * FROM Book";
$result = mysqli_query($link, $query) or die("Error " . mysqli_error($link));
if($result)
{
$rows = mysqli_num_rows($result);
echo "<table>
<tr>
<th>bookid</th>
<th>Bookname</th>
<th>Authorid</th>
</tr>";
for ($i = 0 ; $i < $rows ; ++$i)
{
$row = mysqli_fetch_row($result);
echo "<tr>";
while ($row = mysqli_fetch_row($result))
{
echo "<tr>";
for ($j = 0 ; $j < 3 ; ++$j)
echo "<td>$row[$j]</td>";
echo "</tr>";
}
}
echo "</table>";
mysqli_free_result($result);
}
mysqli_close($link);
?>
</body>
</html>

最佳答案

尝试使用这个查询

SELECT * FROM books ORDER BY id DESC LIMIT 1

关于php - 如何从表中输出必要的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59170678/

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