Programming MySQL Concept of pagination by SQL Query in MySQL

Concept of pagination by SQL Query in MySQL

Concept of pagination by SQL Queries in MySQL

Before digging deep the Concept of pagination using PHP and MySQL, I would like to introduce you with pagination term.

What is pagination?

Pagination is the procedure of separating content into discrete pages.

In simple words, pagination is an automated process to display content in the form of pages which is broken into separate pages to make them more user-friendly. Pagination allows fetching limited data in chunks which makes data fetching fast and user-friendly.

The Pagination Process.

Let’s discuss the pagination process to understand the concept of pagination. In the process of pagination using PHP and MySQL, we divide the data into chunks and display each section on a separate page in a sequence. To do so, we use the SQL LIMIT. Effective use of SQL LIMIT clause page provides very fine pagination. So let’s start understanding with the help of code and examples.

I am taking a table named fruits which has two columns named id and name. The table is extremely simple and contains data of 100 fruit names and their IDs.

If you are not familiar with SQL queries please click here to read.

How to create pagination: the pagination process

To create pagination, we need to use the select statement. First I will do it by simple SQL queries then will integrate the complete code in PHP with MySQL.

To select a list of complete data is quite handy if the data is not huge. But for heavy data, it is a good practice to use pagination and break the data in parts.

Code example to fetch all the data from table fruits:

Now if going towards pagination, there are a few things which are necessary.

Setting page limit and offset for pagination in the concept of MySQL

First is page limit. A page limit is a number defining how many records are to be fetched in a single query and will be displayed on a single page.

Second is order. Records must be fetched in an order. Like ascending order of ID or Fruits name, Descending order of Id or Fruits name. By default, the order will be ascending order of ID.

Query to display records in pagination.

Let’s take a Limit of 10 records. That means total records will be distributed in 10 pages (10 pages x 10records per page = 100 records).

The order I am considering is, ascending order of Fruits name that means records displayed will be alphabetically sorted regardless of ID.

*A better approach to fetch data is by using column names. You should always fetch only required columns, instead of all the columns. In my case, I have only two columns so I am not using column names.

This code will display the first ten records in alphabetical order. Here I am using a limit of 10 with offset 0. Offset in limit defines where to start. The offset for the first row starts with 0 (zero) Like in array index starts with 0(zero).

The core of pagination is offset. If there is no offset used the query will display first ten records every time. However, by changing offset the records starting point changes and it signifies the minimum starting point.

The offset for the second page will be 10. The below query will fetch the second-page records.

Calculate offset for pagination

The calculation for offset is very easy. You can use the below method to calculate the next offset.

NEXT OFFSET = (LAST OFFSET + LIMIT = 0+10 = 10).

In the same way the queries for all the pages from first t last will be:

The result for the queries will be like below images

Result of the first page of pagination by SQL Queries in MySQL
Result of the first page of pagination by SQL Queries in MySQL
Result of the second page of pagination by SQL Queries in MySQL
Result of the second page of pagination by SQL Queries in MySQL
Result of the last page of pagination by SQL Queries in MySQL
Result of the last page of pagination by SQL Queries in MySQL

sample Data for practice

Below is the export query for the table and data I am using in the operations to create the pagination process. You just need to copy the code below and execute the SQL.

I hope you liked this article, please share it with your friends.

Leave a Reply