我在数据库中有一个包含2列的表,id(自动增量)和名称。
id|name
--|----
1 |John
2 |Jeff
3 |Jack
我希望在我的网页中显示最后一个插入名称,以便我echo "The last person is $last_name";
我尝试使用以下命令检索数据,但它什么也没显示。
$last_name = mysqli_query($conn, 'SELECT name FROM namelist ORDER BY id DESC LIMIT 1')
有人请帮忙吗? 谢谢。
答案 0 :(得分:1)
试试这个......
如果是程序
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
mysqli_query($conn, $query);
mysqli_insert_id($conn)
如果面向对象
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);
$mysqli->insert_id
答案 1 :(得分:0)
尝试这个
$last_name = mysqli_query($conn, 'SELECT name FROM namelist ORDER BY id DESC LIMIT 1')
if (mysqli_num_rows($last_name) > 0) {
// output data of row
while($row = mysqli_fetch_assoc($last_name)) {
echo "Name: " . $row["name"];
}
} else {
echo "0 results";
}
答案 2 :(得分:-1)
mysqli_query($conn, 'SELECT name FROM namelist ORDER BY id DESC LIMIT 0,1')