Join TalkersCode Now !

Get Latest Tutorials And Links On Web Development
And Get Existing Offers Via Email

Simple And Best Pagination With PHP, MySQL

Tuesday, Apr 4, 2017

Tags:- PHP MySQL

Pagination technique is used to fetch small amount data stored in a database inspite of fetching all the data at once from the database. It is very usefull technique it load the page fast. You can make Simple and Best Pagination in just 3 Steps.You may also like ajax pagination using PHP and MySQL.

simple and best pagination
  1. Connect To Database.
  2. Query The MySQL Database.
  3. Get The Results.

Step 1.Connect To Database

In first step we have to connect to the database from which you want to get the data. You can connect to any database from which you want to get the data. In this we use sample database named " demo ".

$host = 'localhost';
$user = 'root';
$pass = '';

mysql_connect($host, $user, $pass);


Step 2.Query The MySQL Database.

$offset = 0;
$page_result = 5; 
 $page_value = $_GET['pageno'];
 if($page_value > 1)
  $offset = ($page_value - 1) * $page_result;

$select_results = " select * from student_info limit $offset, $page_result ";

In second step we use three php variables $offset, $page_result, $page_value. $offset = 0 because we want to select the data from 0 positionfrom the table, $page_result = 5 because we want to display 5 results at a time, $page_value = $_GET['pageno'] it is the value of the page when the user wants to change the page and click the pagination page number. Then if the $page_value is greater than 1 we change the $offset value to the value of this code ($page_value - 1) * $page_result. Then we query the database we select details from student_info table and limit the results returned by the query for more details of limit clause visit our MySQL clause tutorial.You may also like load more results from database using ajax.

Step 3.Get The Results.


$result = mysql_query( $select_results );

while($row = mysql_fetch_array($result))
 echo $row[' student_name '];
 echo $row[' student_rollno '];
 echo $row[' student_course '];

$pagecount = 50; // Total number of rows
$num = $pagecount / $page_result ;

if($_GET['pageno'] > 1)
 echo "<a href = 'samepage.php?pageno = ".($_GET['pageno'] - 1)." '> Prev </a>";
for($i = 1 ; $i <= $num ; $i++)
 echo "<a href = "samepage.php?pageno = ". $i ." >". $i ."</a>";
if($num! = 1)
 echo "<a href = 'samepage.php?pageno = ".($_GET['pageno'] + 1)." '> Next </a>";


In this step we get the results from student_info table, in student_info table we have three columns student_name, student_rollno, student_course. then, we use $pagecount = 50 because there are 50 rows in student_info table. Then, we use $num variable and set the value returned by $pagecount / $page_result. The first if statement display the Prev page link if the pageno is greater than 1 and then we display all the pagination links using for loop upto the $num value. The second if statement display the Next page link if the pageno is not equal to 1. Remember always validate data before and after submitting the form.

That's all, this is simple and best way to make Pagination with the help of PHP and MySQL. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email

Are You Starting A New Blog
Do You Want Best Web Hosting?

Try World's Best Web Hosting Provider BlueHost
At Just $3.95/mo

Hurry Up! Limited Time Offer

Yes, I Want To Check Out BlueHost

Check The World's Best SEO And All-In-One Marketing Toolkit
For Digital Marketing Professionals SEMRUSH