PHP 可视化 MySQL select 最后 n 个值

PHP visualize MySQL select last n values

我想在我的网站上显示来自 MySQL 数据库的最后 144 个温度数据。现在我有以下代码:

<?php
$servername = "localhost";
$username = "_accounts";
$password = "---";
$dbname = "_accounts";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT `t` FROM `WbabZwvgf` ORDER BY `id` DESC LIMIT 144";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    $t = $row["t"];
  }
} else {
  echo "0 results";
}
$conn->close(); 

现在,如果我像这样在“while”中声明后回显 $t:

while($row = $result->fetch_assoc()) {
    $t = $row["t"];
    echo $t." ";
  }

似乎一切正常,但我无法将每个值保存为 php 变量,如 $t1=t[0]、$t2=t[1]、$t3=t[2].. .etc,甚至在“while”期间。你能帮我如何将选定的数据保存到 PHP 个变量中吗?

尝试

$t = array();
while($row = $result->fetch_assoc()) {
    $t[] = $row["t"];
    
  }
var_dump($t);

你应该使用数组。但你问题的正确答案是:

$i = 1;
while($row = $result->fetch_assoc()) {
    ${'t'.($i++)} = $row['t'];
}
echo $t1;
echo $t2;

--

感谢您的减分。但请正确阅读他的问题。他要求 $t1、$t2 等等。 现在考虑哪个答案是正确的。带数组的肯定不行