获取重复的条目错误消息并在if语句中使用

I would like to get the error message if a duplicate entry error happened with PDO.

this is the code I use where a duplicate entry is possible because id is a unique key:

$movetotable = $conn->prepare("INSERT INTO `$table` SELECT * FROM `$trashtable` WHERE id = :id");
$movetotable->bindParam(':id', $id, PDO::PARAM_STR);
$movetotable->execute();

I hope it is possible with PHP PDO, I know this: PDO::errorCode() but I simply don't know how to use it in an if statement.

Thanks in advance

$stmt = $conn->prepare("INSERT IGNORE INTO `$table` SELECT * FROM `$trashtable` WHERE id = ?");
$stmt->execute([$id]);
$id = $conn->lastInsertId();
if (!$id) {
    echo "a dupe!";
}