根据ID获取最高的前4行


Get top 4 highest rows based on ID

我试图获取表中最高的4行,并为它们分配变量,以便在整个代码中使用它们。

以下是我到目前为止所能得到的。。。

        <?php
$username="USERNAME";
$password="PASSWORD";
$database="DATABASE";
$conn = mysqli_connect(localhost, $username, $password, $database);
$query = "SELECT * from `shoutbox` ORDER BY `id` DESC LIMIT 4";
$result = $conn->query($query);
    while($row = $result->fetch_assoc()) {
              $name =($row["name"]);
              $message =($row["message"]); 
              $time =($row["time"]); 
    }

?>

如何分配更多变量以获得接下来的3行?

我相信是(对不起,今天很长,所以我可能会休息)

$result = array();
while(...){
      $result[] = $row;
}

我建议您循环遍历结果集,并将每个迭代分配给一个数组,然后您可以稍后从数组中提取值
以下是您的代码的工作更新

// ...
$query = "SELECT * from `shoutbox` ORDER BY `id` DESC LIMIT 4";
$result = $conn->query($query);
// create array to hold queried result set
$output = array();
// loop through result set and assign each row to a key in $output array
// Note: the fetch_assoc() method auto-increments an internal pointer 
// so as you spin through the result set $row will move down the rows
while($row = $result->fetch_assoc()) {
    $output[] = $row;
}
// $output now contains an associative array on each key representing
// each row, so to access each row:
foreach($output as $row) {
    echo $row["name"] . "<br>";
    echo $row["message"] . "<br>";
    echo $row["message"] . "<hr>";
}

$result定义为while 之后的数组

$result = array();
while($row= .... ){
$var= $row['name'];
.............
}