Page 1 of 1
how to find the last record from the table?
Posted: Tue Dec 14, 2004 9:41 am
by kanchan
can u plz tell me how to find the last row from the mysql table?
Posted: Tue Dec 14, 2004 9:47 am
by timvw
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...
Code: Select all
SELECT * FROM table ORDER BY timestamp DESC LIMIT 1
try this ...
Posted: Tue Dec 14, 2004 9:56 am
by neophyte
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.
Posted: Tue Dec 14, 2004 9:57 am
by neophyte
Timvw's too quick...
Posted: Tue Dec 14, 2004 9:59 am
by timvw
you sound like my girlfriend

Re: try this ...
Posted: Tue Dec 14, 2004 9:59 am
by kanchan
Re: try this ...
Posted: Fri Dec 17, 2004 5:45 pm
by kanchan
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.
i got another problem now....
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
Posted: Fri Dec 17, 2004 5:54 pm
by andre_c
add a die statement to the query:
Code: Select all
<?
$result = mysql_query($query) or die( mysql_error() );
?>
i think there may be an error there
Posted: Sat Dec 18, 2004 12:45 am
by kanchan
andre_c wrote:add a die statement to the query:
Code: Select all
<?
$result = mysql_query($query) or die( mysql_error() );
?>
i think there may be an error there
no no.. that's not the error.........
i need how to display the result from the above code
Posted: Sun Dec 19, 2004 3:15 pm
by andre_c
do you get an error when you add the die statement though?
if your query is not executing correctly you can't display the row.
You need to first solve any errors.
Posted: Thu Dec 23, 2004 10:50 am
by kanchan
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