Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
How to create Pagination with PHP and MySql

How to create Pagination with PHP and MySql

Posted on June 3, 2018December 17, 2022 By XpertPhp 4 Comments on How to create Pagination with PHP and MySql

In this tutorial, I will inform you How to create Pagination with PHP and MySql. If we have more data at that time we need to PHP pagination because if we display all data that will take time. in this example, we will create a simple example of pagination in PHP. so you can see our PHP pagination example.

Read Also:
PHP CRUD operation with MySQL Example

Pagination means document content dividing into multiple pages. whenever we have one long page at that time we dividing into multiple pages.

if you want to create pagination using MySQL at that time use limit clause, it takes two arguments first is “offset” and the second is the “end”. end means a number of records returned from the database.

I have share post next and previous button with code in this tutorial and I hope it will be helping you. below example in 3 records displayed per page.

Connect to Database

Create a new connection.php file. The code below is to provide how to connect with the database.
The following code is used to connect MySQL to PHP. It requires a hostname, database username, database password, and database name.

1
2
3
4
5
6
7
8
9
10
<?php
 
$hostname="localhost";
$username="root";
$password="test";
$database="register";
 
$conn = mysqli_connect($hostname,$username,$password,$database);
?>

PHP pagination Example with Mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<html>
<head>
     <title>How to create Pagination with PHP and MySql?</title>
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script
</head>
<body>
 
<table border="1" align="center">
<tr>
     <td colspan="7" align="right"><a href="add.php">Add</a></td>
</tr>
<tr>
     <td>First Name</td>
     <td>Last Name</td>
     <td>Address</td>
     <td>Email</td>
     <td>Mobile</td>
     <td>Action</td>
</tr>
<?php
include('connection.php');
$total=3;
if (isset($_GET["page"]))
{
$page  = $_GET["page"];
}
else
{
$page=1;
}
$start = ($page-1) * $total;
$slt="select * from register LIMIT $start,$total";
$rec=mysqli_query($conn,$slt);
while($row=mysqli_fetch_array($rec))
{
?>
<tr>
   <td><?php echo $row['first_name']; ?></td>
   <td><?php echo $row['last_name']; ?></td>
   <td><?php echo $row['email']; ?></td>
   <td><?php echo $row['address']; ?></td>
   <td><?php echo $row['mobile']; ?></td>
   <td><a href="update.php?edit_id=<?php echo $row['id'];?>">Edit</a>&nbsp;&nbsp;<a href="delete.php?delete_id=<?php echo $row['id'];?>">Delete</a> </td>
</tr>
<?php
}
?>
<tr>
<td colspan="7">
<ul class="pagination">
    <li class="<?php if($page==1){ echo 'disabled';} ?>">
  <a href="<?php if($page==1){ echo '#';} else {?><?php echo $_SERVER['PHP_SELF']?>?page=<?php echo $page-1; }?>" aria-label="Previous">
<span aria-hidden="true">Previous</span>
  </a>
     </li>
      <?php
$slt="select * from register";
$rec=mysqli_query($conn,$slt);
$total1=mysqli_num_rows($rec);
$total_pages = ceil($total1 / $total);
for($i=1;$i<=$total_pages;$i++)
{?>
     <li class="<?php if($_GET["page"]==$i){ echo 'active';  } ?>"><a href="<?php echo $_SERVER['PHP_SELF']?>?page=<?php echo $i;?>"><?php echo $i;?></a>
                             </li>
<?php
}?>
      <li class="<?php if($page==$total_pages){ echo 'disabled'; } ?>">
  <a href="<?php if($page==$total_pages){ echo '#';} else {?><?php echo $_SERVER['PHP_SELF']?>?page=<?php echo $page+1; }?>" aria-label="Next">
<span aria-hidden="true">Next</span>
  </a>
      </li>
</ul>
</td>
</tr>
</table>
</body>
</html>
See also  Multiple image upload using jquery Ajax and Php

Php, MySql Tags:create pagination in PHP, custom pagination, next previous pagination, pagination, Pagination in PHP with MySQL, php pagination, php with mysql

Post navigation

Previous Post: codeigniter select query with multiple clauses
Next Post: How to create Pagination in Codeigniter and MySql

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com