Hi,

I'm new in php and i need a code that show data from the database and limit it to only 5 results per page and if there are more than 5 results, the other results will be in page 2 and the other 11-15 results are in page 3 etc..

Please help me and can you provide an explanation to the code so that i can become more familiar with php..

Thanks for your help in advance.

Dani AI

Generated

— pagination is the right direction. pointed to record-set paging and named it pagination; correctly corrected the LIMIT parameter semantics. Below is a compact, safe pattern you can use with modern PHP plus a few practical tips you will not find in the earlier replies.

A simple workflow: validate the page input, set perPage = 5, compute an offset from the page, query the database for that slice (always include an ORDER BY so results stay deterministic), and run a separate COUNT to calculate total pages. Example using PDO and integer binding (bind limit/offset as integers to avoid injection and driver issues):

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 5;
$offset = ($page - 1) * $perPage;

$stmt = $pdo->prepare('SELECT id, title FROM articles ORDER BY created_at DESC LIMIT :perPage OFFSET :offset');
$stmt->bindValue(':perPage', $perPage, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

$total = (int) $pdo->query('SELECT COUNT(*) FROM articles')->fetchColumn();
$totalPages = (int) ceil($total / $perPage);

Troubleshooting and notes: clamp page to the valid range so users do not request negative or beyond-last pages; avoid SELECT * on large rows; cache or approximate counts for very large tables because COUNT(*) can be slow. For very high offsets, consider keyset (seek) pagination for much better performance — an accessible explanation is at . For reference on SELECT/LIMIT behavior and PDO binding see the official docs: MySQL SELECT and PDOStatement::bindValue.

Recommended Answers

All 3 Replies

you can search about record set paging

The thing u are talking about is known as "Pagination". Its not much easy for the beginners though but you can have a search on google for it.
To limit the displayed records you can simply put this query:
"SELECT * FROM tablename LIMIT 10".
If you want records from 10-20 then you can achieve it by:
"SELECT * FROM tablename LIMIT 10,20".

Member Avatar for Member #120589

If you want records from 10-20 then you can achieve it by:
"SELECT * FROM tablename LIMIT 10,20".

UNfortunately, this isn't right.

If you want records from 10-20 then you can achieve it by:
"SELECT * FROM tablename LIMIT 9,11"

The first number in the LIMIT x,y syntax refers to the offset (x). This means basically, how many records you want to skip before starting. So to start from the first records, you'd have this:

LIMIT 0, y (whatever y would be).

The second number in the LIMIT x,y syntax refers to the actual number of records (y) you want to return from the offset (x). SO:

LIMIT 3, 14

would give start at record 4 and return all records 4 to 17.

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.