while($row=mysql_fetch_array($resultat)) { if($row[0] == $bid) { $nextbid = 1; } else if($nextbid == 1) { /* the next bilder will be $row[0] */ } }