while循环在mysqli_fetch_assoc重复输出

时间:2018-03-10 21:44:42

标签: php sql mysqli

这是我的代码

$sqli = "SELECT * FROM transaction";
$result2 = mysqli_query($connect,$sqli);

while($row = mysqli_fetch_assoc($result2) ) {
   echo "Test : " . $row['id'];

}

输出:测试:192测试:193测试:194测试:195测试:196测试:197

我只想显示最后一个197的ID。我该怎么做?

1 个答案:

答案 0 :(得分:2)

要仅显示一个特定行,请修改SQL以获取该行

$sqli = "SELECT * FROM transaction WHERE id = 197";
$result2 = mysqli_query($connect,$sqli);

$row = mysqli_fetch_assoc($result2);
echo "Test : " . $row['id'];

或正如@Funky所说,如果您只想要表格的最后一行row.id可能

$sqli = "SELECT * FROM transaction 
        ORDER BY id DESC
        LIMIT 1";
$result2 = mysqli_query($connect,$sqli);

$row = mysqli_fetch_assoc($result2);
echo "Test : " . $row['id'];