# prequery to see if there IS a previous/next id my $data = qq(SELECT id, title FROM pictures WHERE category = $category ORDER BY id ASC); my $sth = $dbh->prepare($data); $sth->execute() or die $dbh->errstr; my @pictures_in_category; my ($picid, $pictitle); $sth->bind_columns(\$id, \$title); while($sth->fetch) { push(@pictures_in_category, $id); } # real query for the one image you're viewing my $data2 = qq(SELECT id, filename, title, category, description, date, views, sensorisospeed, exposuretime, isospeedrating, lensaperture, flash, focallength, cameramodel, shutterspeed FROM pictures WHERE id=?); my $sth2 = $dbh->prepare($data); $sth2->execute($picture) or die $dbh->errstr; ...