my $sql = "SELECT UID FROM news"; #### while (my $newUID = $sth->fetchrow) { if ($in{'requestID'} == $newUID) { $currID = $newUID; } else { $prevID = $newUID; } ... #there's a counter to generate one more loop after the currID is found, to capture "nextID" $nextID = $newUID; } print p(["Previous = |$prevID|","Current = |$currID|","Next = |$nextID|"]); #### 0 #this is a counter value to escape the while loop after finding the value of $nextID Previous = |1| Current = || Next = || 1 Previous = |1| Current = || Next = |2|