How to fetch last field of the recordset

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
User avatar
ankurdave
Forum Newbie
Posts: 7
Joined: Tue May 18, 2004 12:35 am
Location: INDIA

How to fetch last field of the recordset

Post by ankurdave »

HI to all
My problem is

I have one table in which one field is id which is having some numbers from 1 to 100 some diff . no. now my qurey is

Code: Select all

select * from ABC order by id
so the table is maintained by the id.

Now i want to fetch only the last id


I simply want to fetch the last id valus so that i can assign the new value in id which is higher then the previous one.


Thnx in ad.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

if you wanna select the last id in the database, you could use this query

Code: Select all

SELECT MAX(id) FROM abc
Mark
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

Post by kevin7 »

or... u can try...

Code: Select all

SELECT * FROM ABC order by id desc
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

SELECT MAX(id) FROM abc
Depending on his table structure, that might not always work. MySQL can often re-use old values - i.e. it might not always be the largest number that was the most recent addition. If he's got ID set to be an auto-inc field, he should be fine :) but you never know...
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

jus to give you an idea

Code: Select all

$result = "// the query";
$num_rows = mysql_num_rows($result);
// now we have last row
// do for loop?? to get last number
mysql_fetch_row???
User avatar
ankurdave
Forum Newbie
Posts: 7
Joined: Tue May 18, 2004 12:35 am
Location: INDIA

Post by ankurdave »

Thnx I got the ans by your views

Thnx to all
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

launchcode wrote:
SELECT MAX(id) FROM abc
Depending on his table structure, that might not always work. MySQL can often re-use old values - i.e. it might not always be the largest number that was the most recent addition. If he's got ID set to be an auto-inc field, he should be fine :) but you never know...
But as he said "I simply want to fetch the last id valus so that i can assign the new value in id which is higher then the previous one." i thought this would suffice. He just want to assign a new unique value from what i understand by the information he gave us.

Mark
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

also if they were using AUTO INCREMENT they wouldn't need to do all of this to get the last id out, it would be done for them. stick with the MAX().
Post Reply