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
monsta-toast
Forum Newbie
Posts: 16 Joined: Sat Jan 17, 2009 3:21 pm
Location: NEWCASTLE UPON TYNE
Post
by monsta-toast » Sun Feb 08, 2009 7:30 am
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
Ziq
Forum Contributor
Posts: 194 Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh
Post
by Ziq » Sun Feb 08, 2009 7:59 am
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
monsta-toast
Forum Newbie
Posts: 16 Joined: Sat Jan 17, 2009 3:21 pm
Location: NEWCASTLE UPON TYNE
Post
by monsta-toast » Sun Feb 08, 2009 8:10 am
no i want to select the last id from the database so i can use the id as a unique file name for images.
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Feb 08, 2009 9:42 am
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
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().
monsta-toast
Forum Newbie
Posts: 16 Joined: Sat Jan 17, 2009 3:21 pm
Location: NEWCASTLE UPON TYNE
Post
by monsta-toast » Sun Feb 08, 2009 10:12 am
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
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?
monsta-toast
Forum Newbie
Posts: 16 Joined: Sat Jan 17, 2009 3:21 pm
Location: NEWCASTLE UPON TYNE
Post
by monsta-toast » Sun Feb 08, 2009 11:08 am
anyone?
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Feb 08, 2009 11:18 am
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.
monsta-toast
Forum Newbie
Posts: 16 Joined: Sat Jan 17, 2009 3:21 pm
Location: NEWCASTLE UPON TYNE
Post
by monsta-toast » Sun Feb 08, 2009 11:26 am
Thanks A million!
abel
Forum Newbie
Posts: 6 Joined: Sun Feb 08, 2009 11:47 am
Post
by abel » Sun Feb 08, 2009 12:24 pm
Code: Select all
$query="SELECT max(ID) FROM inventory limit 0,1";
right?
Kastor
Forum Newbie
Posts: 24 Joined: Thu May 01, 2008 2:29 am
Location: Grodno, Belarus
Contact:
Post
by Kastor » Sun Feb 08, 2009 12:50 pm