OPDATERING. Af en af har jeg lært, at alt kan gøres i én forespørgsel
$sql = 'select 1 from table where id = ? and self <=> ? and parent <=> ?';
$stm = $conn->prepare($sql);
$stm->execute([$id,$self,$parent]);
$exists = $stm->fetchColumn();
Du skal også ændre din forespørgsel.
$sql = 'select count(id) as exists from table where id = ? and ';
if ($type == 1) {
$sql .= 'self IS NULL and parent IS NULL'
$data = [$id];
} else {
$sql .= 'self = ? and parent = ?';
$data = [$id,$self,$parent];
}
$stm = $conn->prepare($sql);
$stm->execute($data);