我只需显示行中的最后一个值。现在显示
消息:Rocha:gff 消息:Rocha: 消息:Rocha:我的名字是肯尼
我只需要它显示消息:Rocha:我的名字是肯尼。 谢谢
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "company";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, className, lastname, messages FROM Mymesages";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if("CPS210-CompSci-I (4)"==$row["className"] && $lastname== $row["lastname"]){
echo "Message for: " . $row["lastname"]. " : " . $row["messages"]. "<br>";
}
}
}
$conn->close();
?>
答案 0 :(得分:1)
如果要显示最后一行,则查询应如下所示:
$sql = "SELECT id, className, lastname, messages FROM Mymesages ORDER BY id DESC LIMIT 1";
之后,而不是while
循环只需像这样获取行:
$row = $result->fetch_assoc();
答案 1 :(得分:1)
如果您只查找一条记录,那也是最后一条记录,您只需要稍微修改一下您的查询。此外,在这种情况下也不需要循环。
$sql = "SELECT id, className, lastname, messages FROM Mymesages ORDER BY id DESC LIMIT 1";
替换此行:
while($row = $result->fetch_assoc()) {
简单地说:
$row = $result->fetch_assoc();