OK the question is NOT about how to return the ID, but more like how to skip it… I have a situation where i use last_insert_id() to get the id of the last inserted row, but i want to get a zero if no row is inserted. The problem is as follows:
- i insert…update a few records: all IDs are returned since the rows are freshly inserted
- then i repeat the process: this time IDs are not returned. YAY! that’s exactly what i want
- i change one column value and repeat the process: but now the ID is returned for the row i’ve changed the column value in. And this is making me very angry. I don’t want mysql to return the ID at all if it’s not inserting anything
so to sum up:
- when inserting new records: IDs are returned
- when inserting identical records: IDs are not returned
- when updating a value: updated IDs of the updated rows are returned 🙁
Is there a workaround for this?
I know this happens because mysql probably does not perform an update on identical records, but still … i’d sure like to know how to work around this.
You should be able to retrieve the number of rows inserted and use that to determine if anything was inserted. If you’re using “raw” queries (i.e. no API that might otherwise provide the number of rows inserted), it looks like ROW_COUNT() might give you what you want.