// Created on savesnippets.com ยท https://savesnippets.com/KTdsl7DYJeLPnx prepare($sql); $stmt->execute($afterId === null ? [] : [$afterId]); $rows = $stmt->fetchAll(); return [ 'rows' => $rows, 'cursor' => $rows ? end($rows)['id'] : null, // pass back as ?afterId=... next time ]; } $first = fetchAfter($db, null); // newest 25 $second = fetchAfter($db, $first['cursor']); // next 25