如何使用PHP获取MySQL数据库表中的最后一条记录? [英] How do I fetch the last record in a MySQL database table using PHP?

查看:994
本文介绍了如何使用PHP获取MySQL数据库表中的最后一条记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PHP获取MySQL数据库表中的最后一个结果。我将如何做这个?

I want to fetch the last result in MySQL database table using PHP. How would I go about doing this?

我在表中有2列,MessageID(auto)&

I have 2 Columns in the Table, MessageID(auto) & Message.

我已经知道如何连接到数据库。

I already know how to connect to the database.

推荐答案

p>使用 mysql_query

<?php
$result = mysql_query('SELECT t.messageid, t.message 
                         FROM TABLE t 
                     ORDER BY t.messageid DESC 
                        LIMIT 1') or die('Invalid query: ' . mysql_error());

//print values to screen
while ($row = mysql_fetch_assoc($result)) {
  echo $row['messageid'];
  echo $row['message'];
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);

?>

SQL查询:

  SELECT t.messageid, t.message 
    FROM TABLE t 
ORDER BY t.messageid DESC 
   LIMIT 1

...使用ORDER BY设置值,因此最高的值是resultset中的第一行。 LIMIT说,在所有这些行中,只有第一行实际返回的结果集。因为 messageid 是自动递增,最高值是最近的一个...

...uses the ORDER BY to set the values so the highest value is the first row in the resultset. The LIMIT says that of all those rows, only the first is actually returned in the resultset. Because messageid is auto-increment, the highest value is the most recent one...

这篇关于如何使用PHP获取MySQL数据库表中的最后一条记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆