deleting the last inserted row in DB

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
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

deleting the last inserted row in DB

Post by thallish »

hey all

I have a dream that it one day would be possible to use the following code snippet

Code: Select all

// the newly inserted row in table 'education' is deleted
$query = &quote;DELETE FROM table WHERE id=MAX(id)&quote;;
but it's not working. Probably because of the wrong use of the MAX-function(at least that's what mysql_error() tells me)

So is it really necessary to count the rows, set $id=count_result and then delete where id='$id', or is there another smarter way of doing it?

thallish
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

if your id is auto-increment then you can get last id generated by

Code: Select all

SELECT LAST_INSERT_ID()
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

FYI, number of records and the ID have little to do with each other. Reason: delete a record. You'll have a discrepancy.
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post by thallish »

yeah know that :wink:

just explained it akwardly. It was meant to be the last inserted row == the one with the highest id.

I might as well ask on a suggestion here. I have some tables I want to put some data into. If one insert gets wrong i'll have to correct my previous inserts. Is there a best way to do that? My db doesn't support INSERT ALL

thallish
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Post Reply