I basically want to insert a record and return the id. I'm using mysql_insert_id(). However, I'm stuck at one part. If the record is a duplicate, it returns a THE NEXT autoincrement id. I want it to return the id of the record thats a duplicate. So if record exists and its id is 57, I want it to return 57, OTHERWISE return the new id of the inserted element. Any help? Armin
You could run a query to search for a duplicate before you insert. Then you can easily retrieve that variable, and still continue to insert and grab mysql_insert_id();
Or instead of mysql_insert_id, afterwards just run a query, order by ID in ascending order and limit by 1 result. Would return the ID you're after.
Hi guys, thanks for the help, but the whole point of this is to AVOID extra queries. Its easy to query for the ID, but my question is WHY does it not return the existing id when a duplicate record exists, instead it returns a NEW auto_increment id, even though the record already exists.
Yes, I know that. I wish this was the case. mysql_insert_id in my case grabs the id of the NEW RECORD that is NOT BEING INSERTED. If there are records 1,2,3 and I'm trying to insert record 2, it would return 4!!! NOT 2!!
Am i missing a point to this? php built in a practical, useful function. If you want another php function to do the leg work, then make one. Surely mysql returns an error when you try to insert over an existing id. You might then be able to parse that error to get the id. Also, if you are trying to insert over a duplicate id, then you must already know it. Your method is not logical, perhaps i have misunderstood.