Page 1 of 1

Retrieving the last inserted row

Posted: Mon Jul 20, 2009 7:47 pm
by flycast
I have a table that does not have any indexes defined and no auto incrementing fields. Even worse is that I can insert two rows with the same data in the fields so that the rows are exactly identical. If I use mysql_insert_id() I get a zero because it is missing an autoincriment field. Is there a reliable way I can get a piece of data that I can use later to go back and delete that specific row without modifying the table structure?

Re: Retrieving the last inserted row

Posted: Mon Jul 20, 2009 11:04 pm
by Benjamin
Yes but you should just add an auto_increment primary key and be done with it rather than write bandaid code.

Re: Retrieving the last inserted row

Posted: Tue Jul 21, 2009 9:17 pm
by flycast
astions wrote:Yes but you should just add an auto_increment primary key and be done with it rather than write bandaid code.
No. It is an add-in to a CMS. In order for the add-in to work it must deal with the tables that are already defined as is.

Re: Retrieving the last inserted row

Posted: Tue Jul 21, 2009 9:41 pm
by Benjamin
Adding fields to database tables should not affect the behaviour of code already using those tables.

Go ahead and post the table schema though. Then we can give you some specific answers to your issue.

Re: Retrieving the last inserted row

Posted: Tue Jul 21, 2009 9:47 pm
by superdezign
In order to do what you want, the clearest way is to add another column to it anyway. The column would make it easy to select the newest column. The best way to do it is to add an auto_increment id field. If there is some sort of unique id field in the table that you need to define the value of yourself, then you could simulate an auto_increment, but then why not just use an auto_increment id field?

The best method is either an auto_increment field or a date/datetime field. Both solutions require altering the database table, which is not a harmful process.