03/07/2021
PHP PAGINATION
In this tutorial, i will explain what PHP PAGINATION is and how we can create simple pagination using PHP and MySQLi. If you actually work with SQL workbench and fetch multiple records from database, then you have seen that it is not good idea to list all the records in a very single page specially when your records are thousands in number. So instead of that with inchoate PHP PAGINATION just to make it easier. the best approach is to split these records into multiple pages so that user can easily view or read these records.
What is Pagination?
Pagination mean fetching and displaying your data into multiple pages rather than single page. You have already seen this on various blogs, even on my blog homepage, you can see that I am only displaying 4 to 5 blog posts and older posts are accessible via pagination.
How to Create Simple Pagination Using PHP and MySQLi
Basically we need to fetch limited records on each page, this mean we need to limit the number of records to be fetched. For this purpose, MySQL provides a LIMIT clause, it simply fetch the limited number of records. I have created a sample table named `pagination_table` which you can download from the download button and import into your database. Lets see how LIMIT clause works, just run the following query.
SELECT * FROM `pagination_table` LIMIT 3;
It will give you the following result.
As you can see above that it only returns 3 records, however table contain total 36 records in it. But if we need to fetch the records starting from 5th to 7th then we will use OFFSET in MySQL query, there are two different ways to use the OFFSET, however both will return the same records, you can use anyone that you like.
SELECT * FROM `pagination_table` LIMIT 3 OFFSET 4;
Or you can use shorthand method as well.
SELECT * FROM `pagination_table` LIMIT 4, 3;
These both queries will return the following records.
The difference between both queries that in first method you write LIMIT value after LIMIT and OFFSET value after OFFSET, but in shorthand method you write OFFSET value after LIMIT clause and LIMIT value after