how do you select the last id in my sql 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
User avatar
monsta-toast
Forum Newbie
Posts: 16
Joined: Sat Jan 17, 2009 3:21 pm
Location: NEWCASTLE UPON TYNE

how do you select the last id in my sql table?

Post by monsta-toast »

hi what is the correct code for selecting the last row id in mysql table?

Code: Select all

$query="SELECT max(ID) FROM inventory ";
i tried this but it doesn't work :banghead:
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: how do you select the last id in my sql table?

Post by Ziq »

Do you have some errors?

Why are you using this query? If you want to insert data into database you should use the auto_increment attribute. MySQL Manual
User avatar
monsta-toast
Forum Newbie
Posts: 16
Joined: Sat Jan 17, 2009 3:21 pm
Location: NEWCASTLE UPON TYNE

Re: how do you select the last id in my sql table?

Post by monsta-toast »

no i want to select the last id from the database so i can use the id as a unique file name for images.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: how do you select the last id in my sql table?

Post by John Cartwright »

monsta-toast wrote:hi what is the correct code for selecting the last row id in mysql table?

Code: Select all

$query="SELECT max(ID) FROM inventory ";
i tried this but it doesn't work :banghead:
This will give you the highest ordered ID, so yes it does work. Please post the relevant code and errors describing why this doesn't work.

If you are inserting a row in the database and simply want it's ID back, you can use mysql_insert_id().
User avatar
monsta-toast
Forum Newbie
Posts: 16
Joined: Sat Jan 17, 2009 3:21 pm
Location: NEWCASTLE UPON TYNE

Re: how do you select the last id in my sql table?

Post by monsta-toast »

John Cartwright wrote:
monsta-toast wrote:hi what is the correct code for selecting the last row id in mysql table?

Code: Select all

$query="SELECT max(ID) FROM inventory ";
i tried this but it doesn't work :banghead:
This will give you the highest ordered ID, so yes it does work. Please post the relevant code and errors describing why this doesn't work.

If you are inserting a row in the database and simply want it's ID back, you can use mysql_insert_id().
This function returns 0 if the previous operation does not generate an AUTO_INCREMENT ID, or FALSE on MySQL connection failure. so how do i get the last id and add 1 to it? then use the id created as a file name?
User avatar
monsta-toast
Forum Newbie
Posts: 16
Joined: Sat Jan 17, 2009 3:21 pm
Location: NEWCASTLE UPON TYNE

Re: how do you select the last id in my sql table?

Post by monsta-toast »

anyone? :banghead:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: how do you select the last id in my sql table?

Post by John Cartwright »

Are you asking how to execute the query?

Code: Select all

$query="SELECT max(ID) AS `maxid` FROM inventory ";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo $row['maxid'];
Also, please do not bump your post within 24 hours of your last post.
User avatar
monsta-toast
Forum Newbie
Posts: 16
Joined: Sat Jan 17, 2009 3:21 pm
Location: NEWCASTLE UPON TYNE

Re: how do you select the last id in my sql table?

Post by monsta-toast »

Thanks A million!
abel
Forum Newbie
Posts: 6
Joined: Sun Feb 08, 2009 11:47 am

Re: how do you select the last id in my sql table?

Post by abel »

Code: Select all

$query="SELECT max(ID) FROM inventory limit 0,1";
right?
User avatar
Kastor
Forum Newbie
Posts: 24
Joined: Thu May 01, 2008 2:29 am
Location: Grodno, Belarus
Contact:

Re: how do you select the last id in my sql table?

Post by Kastor »

Code: Select all

SELECT LAST_INSERT_ID()
Post Reply