how to find the last record from the table?
Moderator: General Moderators
how to find the last record from the table?
can u plz tell me how to find the last row from the mysql table?
how would you define last?
to find the last/biggest/smallest in a collection, you need to have an orderrelation. if you apply that orderrelation to the collection, you know that the first (or the last) item is the biggest/smallest.
for example you have a collection of timestamps... if you order them descending you know the first one will be the most recent..
offcourse you don't want all the timestamps, you want to limit to one... because you want only the most recent... therefore mysql has the limit clause...
to find the last/biggest/smallest in a collection, you need to have an orderrelation. if you apply that orderrelation to the collection, you know that the first (or the last) item is the biggest/smallest.
for example you have a collection of timestamps... if you order them descending you know the first one will be the most recent..
offcourse you don't want all the timestamps, you want to limit to one... because you want only the most recent... therefore mysql has the limit clause...
Code: Select all
SELECT * FROM table ORDER BY timestamp DESC LIMIT 1try this ...
Execute a query like this:
SELECT * FROM phpbb_users ORDER BY user_id DESC LIMIT 1;
Or if you are want the last id you can use mysql_insert_id() following an insertion.
SELECT * FROM phpbb_users ORDER BY user_id DESC LIMIT 1;
Or if you are want the last id you can use mysql_insert_id() following an insertion.
Re: try this ...
neophyte wrote:Execute a query like this:
SELECT * FROM phpbb_users ORDER BY user_id DESC LIMIT 1;
Or if you are want the last id you can use mysql_insert_id() following an insertion.
thanx neophyte and timvw
Re: try this ...
i got another problem now....neophyte wrote:Execute a query like this:
SELECT * FROM phpbb_users ORDER BY user_id DESC LIMIT 1;
Or if you are want the last id you can use mysql_insert_id() following an insertion.
how to display the record now....
my code goes here.....
<?
$db=mysql_connect("localhost","root","pwd") or die ("cant connect");
mysql_select_db("phpbb",$db) or die ("cant change");
$query = "SELECT * FROM phpbb_users ORDER BY user_id DESC LIMIT 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$d = $row['name'];
echo $d;
}
?>
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in index.php on line 6
- andre_c
- Forum Contributor
- Posts: 412
- Joined: Sun Feb 29, 2004 6:49 pm
- Location: Salt Lake City, Utah
add a die statement to the query:
i think there may be an error there
Code: Select all
<?
$result = mysql_query($query) or die( mysql_error() );
?>no no.. that's not the error.........andre_c wrote:add a die statement to the query:i think there may be an error thereCode: Select all
<? $result = mysql_query($query) or die( mysql_error() ); ?>
i need how to display the result from the above code
my code is here
<?
$db=mysql_connect("localhost","root","pwd") or die ("cant connect");
mysql_select_db("phpbb",$db) or die ("cant change");
$query = "SELECT * FROM phpbb_users ORDER BY user_id DESC LIMIT 1";
$result = mysql_query($query);
?>
but i don't know how to ECHO the result.... by the way i want to echo the name from that table
<?
$db=mysql_connect("localhost","root","pwd") or die ("cant connect");
mysql_select_db("phpbb",$db) or die ("cant change");
$query = "SELECT * FROM phpbb_users ORDER BY user_id DESC LIMIT 1";
$result = mysql_query($query);
?>
but i don't know how to ECHO the result.... by the way i want to echo the name from that table