In this tutorial, I will teach you how to create pagination using jQuery , Ajax ,PHP</strong> and MySQL database.
It’s quite the same in my last tutorial the only thing is, I added Ajax and I also make some changes in the code.
Ajax is a method that can communicate to a server for exchanging data and updating parts of a web page – without refreshing the entire page.
Let’s begin:
1.Create a MySQL database and name it “peopledb“.
2.Do this following query for creating a table in the MySQL database.
<div class="highlight">
<pre><code class="language-php">CREATE TABLE `peopledb`.`tblpeople` (
`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`NAME` VARCHAR( 30 ) NOT NULL ,
`ADDRESS` VARCHAR( 60 ) NOT NULL ,
`SEX` VARCHAR( 11 ) NOT NULL ,
`CONTACT` VARCHAR( 15 ) NOT NULL
) ENGINE = INNODB;</code></pre>
</div>3.Create a file for a configuration between MySQL database and PHP script. Name it “config.php”.
<?php
$server = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'peopledb';
$con = mysql_connect($server, $dbuser, $dbpass);
if (isset($con)) {
# code...
$dbSelect = mysql_select_db($dbname);
if (!$dbSelect) {
echo "Problem in selecting database! Please contact administraator";
die(mysql_error());
}
} else {
echo "Problem in database connection! Please contact administraator";
die(mysql_error());
}
?>4.Create a css file for the layout of your design. Name it “style.css”.
<div class="highlight">
<pre><code class="language-css">
.table {
width: 100%;
max-width: 100%;
margin-bottom: 20px;
}
.table > thead > tr > th,.table > tbody > tr > td {
padding: 8px;
line-height: 2;
vertical-align: top;
border-top: 1px solid #eee;
}
.table > thead > tr > th {
text-align: left;
vertical-align: bottom;
border-bottom: 1px solid #eee;
}
#loader {
max-width: 100%;
text-align: center;
}
#loader > img {
width: 20%;
height: 30%;
}</code></pre>
</div>5.Set these codes for the design in the first load of the page. Name it “index.php”.
<div class="highlight">
<pre><code class="language-html"><!DOCTYPE html>
<html>
<head>
<!-- css extension -->
<link href="css/style.css" rel="stylesheet" type="text/css">
<title>Itsourcecode</title>
</head>
<body>
<!-- container -->
<div id="wrap">
<!-- header -->
<h1 class="page-header">Pagination with Jquery, PHP and MySQL
Database</h1><!-- Load content -->
<div id="content">
<?php
include 'loaddata.php';
?>
</div><!-- for paging -->
<?php
$perPage = 3;
$sqlQuery = "SELECT * FROM `tblpeople`";
$result = mysql_query($sqlQuery);
$maxrow = mysql_num_rows($result);
$page_number = ceil($maxrow / $perPage)?>
<ul id="pagination">
<?php
//Load page numbers
for ($i = 1; $i <= $page_number; $i++) {
echo '<li id="' . $i . '">' . $i . '</li>';
}
?>
</ul>
</div>
<script src="jquery/jquery.min.js" type="text/javascript">
</script> <!-- jquery extension -->
<script src="js/paging.js" type="text/javascript">
</script><!-- paging extension -->
</body>
</html></code></pre>
</div>6.Create a script for the method of pagination. Name it “paging.js”.
<div class="highlight">
<pre><code class="language-javascript">
$.ajax({
type: "GET",
url: "loaddata.php",
dataType: "text", //expect html to be returned
data: {
page: +page_number
},
success: function(data) {
$("#content").html(data);
}
});
});
});</code></pre>
</div>7.Do the following code for the method in loading the data. Name it “loaddata.php”
<div class="highlight">
<pre><?php
include 'config.php';
?>
<div id="loader">
<!-- -->
<table class="table" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Sex</th>
<th>Contact#</th>
</tr>
</thead>
<tbody>
<?php
$perPage = 3;
if ($_GET) {
$page = $_GET['page'];
} else {
$page = 1;
}
$firstPage = ($page - 1) * $perPage;
$sqlQuery = "SELECT * FROM `tblpeople` limit {$firstPage},{$perPage}";
$result = mysql_query($sqlQuery) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>' . $row['NAME'] . '</a></td>';
echo '<td>' . $row['ADDRESS'] . '</td>';
echo '<td>' . $row['SEX'] . '</td>';
echo '<td>' . $row['CONTACT'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div></pre>
</div>Frequently Asked Questions
How does this PHP project work?
Built with vanilla PHP (no framework) and MySQL backend. Standard structure: form HTML, PHP script handlers, MySQL via PDO or mysqli, sessions for auth, Bootstrap for responsive layout. Ready to extend for BSIT capstone scope.
What PHP and MySQL versions does this project require?
Most projects in this batch run on PHP 7.4 to PHP 8.2 with MySQL 5.7+ or MariaDB 10+. A few older projects need PHP 5.6 (deprecated, use XAMPP 7.x). To run: install XAMPP / WAMP / Laragon, extract project to htdocs, import the included .sql file via phpMyAdmin, edit the connection file (usually config.php or db_connect.php) with your DB credentials, browse to the project URL in your browser.
How do I set up the database for this PHP project?
Open phpMyAdmin (http://localhost/phpmyadmin/ on XAMPP), create a new empty database with the name specified in the project’s config.php. Click the Import tab, choose the included .sql file, click Go. Then edit config.php (or includes/connection.php) with: ‘localhost’, your MySQL username (usually ‘root’), your MySQL password (usually blank for XAMPP), and the database name.
Can I use this PHP project for a BSIT capstone or thesis?
Yes, but extend it. A bare CRUD app is too narrow for full capstone scope. Add: user roles via session checks, reports/dashboards (Chart.js + AJAX), PDF exports (TCPDF library), email notifications (PHPMailer), real domain extension (analytics, audit log, multi-branch support). Pair with Chapter 1-5 documentation matching your panel’s rubric.
Why am I getting ‘connection error’ or ‘undefined function mysqli_connect’?
Three common PHP issues: (1) Connection error: Apache + MySQL services not running in XAMPP control panel, OR database name in config.php does not match what you created in phpMyAdmin. (2) ‘undefined function mysqli_connect’: MySQL extension not enabled, in php.ini uncomment extension=mysqli (then restart Apache). (3) ‘No such file or directory’: MySQL socket path wrong, use 127.0.0.1 instead of localhost in the connection string.
Where can I find more PHP projects with source code?
Browse the PHP Projects hub for the full library (300+ vanilla PHP systems). For modern PHP MVC alternatives see Laravel Projects (74 systems) or CodeIgniter Projects (32 systems). For BSIT-focused capstone idea lists see 150 Best Capstone Project Ideas.
