View Full Version : finding record with the highest ID

Chris Hunter
11-07-2002, 12:16 PM
hot mornig people!

since yesterday i trie to find a solzution for my problem:

i am searching for a posibility to find the record with the highest id


i have a couple of records for displaying members of my community.the position in the list is definied by the variable POSITION. if i want to add a member i need to find the last member or the highest position id to init the new record with (highest position value+1)

but i dont know the query for finding the record with the highest position value. reading the select syntax @ mysql.com has not helped me.

anyone has a glue?

11-07-2002, 12:23 PM
If you set 'id' to auto-increment, mysql would just add one to the highest value when you inserted a blank.

$addthis = mysql_query("SELECT id FROM `table` ORDER BY id DESC LIMIT 1");

Chris Hunter
11-07-2002, 12:26 PM
huuu thanks. that was easy. i am very new in PHP+MySQl so i forgott about the LIMIT option... *g* thanks...

11-07-2002, 03:25 PM
Sorting all the records is probably not the most efficient way. I would use the max function:


Chris Hunter
11-07-2002, 03:29 PM
and how do i get the max value. cause the content of $result is just record #2 or something like that. are there further functions for getting the position value out of the result?