how to find the last record from the table?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

how to find the last record from the table?

Post by kanchan »

can u plz tell me how to find the last row from the mysql table?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

try this ...

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Timvw's too quick...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

you sound like my girlfriend :P
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Re: try this ...

Post 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.

thanx neophyte and timvw :D :D :D :D :D :D
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Re: try this ...

Post 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
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Post 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
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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.
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Post 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
Post Reply