根据ID获取前4个最高行

Get top 4 highest rows based on ID

我正在尝试获取 table 中最高的 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;
}

在 while

之后将 $result 定义为数组
$result = array();
while($row= .... ){
$var= $row['name'];
.............
}

我建议您遍历结果集并将每次迭代分配给一个数组,然后您可以稍后将值从数组中拉回。
这是对您的代码的有效更新

// ...
$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>";
}