PHP MySQL DataTables Server-side Processing Example

By Hardik Savani May 14, 2024 Category : PHP jQuery MySql Ajax

Today, i am going to share with you how to use jQuery datatable plugin in php mysql project. i will also describe small example of data table with server side scripting, here i will use PHP and MySQL to get data from server side. So you have to simple follow few step and will get small quick example.

DataTables is a jquery library that provide pagination, instant search and multi-column ordering to HTML table. DataTables easily use with normal jquery ui project, bootstrap. We can simply get records from server side using ajax.

In this example, i created "users" table with id, name, email field in mysql database. i will create index.html page run ajax request to server and server side fetch that data and display on that page using datatables. It is very simple example, using this you can make more complex too.

So after run example successfully, you will get layout like as bellow attach screen shot. Just follow few step and get layout.


Step 1: Create users table

In first step we will create new new table "users" in database. You can use following SQL Query for create "users" table, after create "users" table make sure you have to add some dummy records. So let's create using bellow sql query:

users table:

CREATE TABLE `users` (

`id` int(10) UNSIGNED NOT NULL,

`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,

`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Step 2: Create config.php File

In this step, we require to create database configuration file, here we will set database name, username and password. So let's create "config.php" file on your root directory and put bellow code:



define (DB_USER, "root");

define (DB_PASSWORD, "root");

define (DB_DATABASE, "phpdemo");

define (DB_HOST, "localhost");

$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);


Step 3: Create index.html File

In this step, we will create index.html file, in this file we will write code for display data using datatables library. So let's create index.html file on your root directory and put bellow code.


<!DOCTYPE html>



<title>PHP - Jquery Datatables Example</title>

<script type="text/javascript" src=""></script>

<link rel="stylesheet" type="text/css" href="">

<script type="text/javascript" charset="utf8" src=""></script>



<div class="container">

<h2>PHP - Jquery Datatables Example</h2>

<table id="my-example">











<script type="text/javascript">

$(document).ready(function() {


"bProcessing": true,

"sAjaxSource": "pro.php",

"aoColumns": [

{ mData: 'id' } ,

{ mData: 'name' },

{ mData: 'email' }






Step 4: Create pro.php File

In last step, we need to create pro.php file, in this file we will write sql query and fetch records from mysql database.




$sql = "SELECT * FROM users";

$result = $mysqli->query($sql);

while($row = $result->fetch_array(MYSQLI_ASSOC)){

$data[] = $row;


$results = ["sEcho" => 1,

"iTotalRecords" => count($data),

"iTotalDisplayRecords" => count($data),

"aaData" => $data ];

echo json_encode($results);


Let's run bellow command on your root directory for quick run:

php -S localhost:8000

Now you can open bellow URL on your browser:


I hope it can help you....