I am using a really awesome piece of code that inserts AND on duplicate key, it updates the duplicate id and gets the last insert id regardless of duplicate or not. Therefore I get the id of the last updated or the duplicate id. But is there a way to distinguish the two?
$prepare=$connection->prepare("
insert into category(related,text) values(?,?)
on duplicate key update id=last_insert_id(id)");
$prepare->execute([1,"property"]);
print_r($connection->lastinsertid());
// this is both the update id and duplicate update id
This will give me an ID regardless of duplicate error or not. But I would like to distinguish if the id is a duplicate or not. Something like:
$prepare=$connection->prepare("
insert into category(related,text) values(?,?)
on duplicate key select id");
$prepare->execute([1,"property"]);
$fetch=$prepare->fetch();
if(!empty($fetch)){
echo 'DUPLICATE';
$id=$fetch['id'];
// this is the duplicate update id
}
else{
$id=$connection->lastinsertid();
// this is the update id
}