Retrieving the last inserted row
Moderator: General Moderators
Retrieving the last inserted row
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
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
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.astions wrote: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
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.
Go ahead and post the table schema though. Then we can give you some specific answers to your issue.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Retrieving the last inserted row
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.
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.