i already have a running code regarding this, however, when i embed the database, it cannot access the next records...help pls..

Dani AI

Generated

As and pointed out, the quickest way to get a useful answer is a minimal reproducible example. For the symptom "cannot access the next records" almost always comes from one of these causes: the page parameter is missing or overwritten, the LIMIT offset is calculated incorrectly, there is no deterministic ORDER BY, or the query is never run because of an include/variable scope issue. A small, safe example that covers the usual flow follows.

<?php
$perPage = 10;
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
if ($page < 1) $page = 1;
$offset = ($page - 1) * $perPage;

$mysqli = new mysqli('localhost','user','pass','db');
if ($mysqli->connect_errno) { die('Connect error: '.$mysqli->connect_error); }

$sql = "SELECT id, title FROM articles ORDER BY id ASC LIMIT $offset, $perPage";
$res = $mysqli->query($sql);

while ($row = $res->fetch_assoc()) {
    echo '<div>'.htmlspecialchars($row['title']).'</div>';
}

$total = (int) $mysqli->query("SELECT COUNT(*) FROM articles")->fetch_row()[0];
$totalPages = max(1, (int) ceil($total / $perPage));
for ($i = 1; $i <= $totalPages; $i++) {
    echo '<a href="?page='.$i.'">'.$i.'</a> ';
}
?>

Debugging tips: echo or log the generated SQL to confirm LIMIT $offset, $perPage values; var_dump($_GET) to make sure page arrives; ensure included files do not reset the $_GET value; always use a deterministic ORDER BY so pages return stable results; cast page to int to avoid injection. If the offset exceeds total rows you’ll get zero results — clamp the requested page to the last page or redirect.

For large tables, avoid very large offsets (performance hit). Use indexed cursor-style pagination (WHERE id > :last_id ORDER BY id LIMIT n) instead. See mysqli::query and MySQL SELECT/LIMIT details in the MySQL manual. — consolidate a single test page with the example above and post that code if it still fails.

Recommended Answers

All 3 Replies

Reuse an old thread maybe?

You have 4 other threads on the same subject on the first 2 pages of this forum....

As for the query, post some code or we will be guessing at the problem.

Show us the code, show us the code.
Where's the code, give us that yum yummy code !!

Member Avatar for Member #120589

Which thread are we supposed to be answering? This one or one of your myriad others? Pagination with CSS, Pagination with php, Pagination with MySQL, Pagination period!

OK, I take it that this is the official thread now...

i already have a running code regarding this, however, when i embed the database, it cannot access the next records...help pls..

pls help by posting code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.