# DataTables Example – Server-Side Processing With PHP

{% file src="<https://3988971916-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LXUV09EJOZ8BKB8YL5I%2F-LzzMpF-3BcQY4AJLf93%2F-LzzN-dtZ8gqcuqJncRz%2Fsimple_datatable_example_code.zip?alt=media&token=4d0821c5-a3ce-464c-ab12-45b863be0f66>" %}

## DataTables Example – Server-Side Processing With PHP

Last Updated On: [October 22, 2017](https://www.phpflow.com/php/datatables-example-server-side-processing-with-php/)| By: [Parvez](https://www.phpflow.com/author/admin/)

In previous post [Data Table jQuery Plugin](https://www.phpflow.com/jquery/data-table-table-plug-in-for-jquery/), we have learn what is jQuery datatable plugin and how to use jQuery datatable in your application,now in this tutorial i will describe how to use data table with server side scripting.I am using PHP and MySQL to get records from server side.In this tutorial our aim to get data from MySQL with help of php and passed data to jQuery datatable constructor.

> Also Checkout other tutorial of Datatable,

* [Part 1: Introduction of Datatable](https://www.phpflow.com/jquery/data-table-table-plug-in-for-jquery/)
* [Part 2- DataTables Example – Server-side Processing with PHP](https://www.phpflow.com/php/datatables-example-server-side-processing-with-php/)
* [Part 3 – How to add RowId and RowClass on each record using Datatable](https://www.phpflow.com/jquery/how-to-add-rowid-and-rowclass-on-datatable/)
* [Part 4 – Datatable Pagination, Sorting and Search – Server Side (PHP/MySQl) Using Ajax](https://www.phpflow.com/php/datatable-pagination-sorting-and-search-server-side-phpmysql-using-ajax/)
* [Part5 – Export the jQuery Datatable data to PDF,Excel,CSV and Copy](https://www.phpflow.com/php/how-to-export-the-jquery-datatable-data-to-pdf-excel-csv-and-copy/)
* [Part 6 – Datatable Responsive – Using PHP and Mysql with Ajax](https://www.phpflow.com/php/datatable-responsive-using-php-and-mysql-with-ajax/)

![datatable-with-php](https://www.phpflow.com/wp-content/uploads/2014/09/datatable-with-php-300x108.png)

#### There Are Following Steps Need To Achieve Over Goal:

**Step 1:** we will include jquery datatable and jquery library.

| 12345678 | \<!-- DataTables CSS -->\<link rel="stylesheet" type="text/css" href="[http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">\\](http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">\\)<!-- jQuery -->\<script type="text/javascript" charset="utf8" src="[http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js">\\](http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js">\\)</script>\<!-- DataTables -->\<script type="text/javascript" charset="utf8" src="[http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js">\\](http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js">\\)</script> |
| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

**Step 2:** Created HTML table in your web page.

| 123456789101112131415161718192021 | \<div class=""> \<table id="example" class="display" width="100%" cellspacing="0">        \<thead>            \<tr>                \<th>Empid\</th>                \<th>Name\</th> \<th>Salary\</th>                          \</tr>        \</thead>        \<tfoot>            \<tr>               \<th>Empid\</th>                \<th>Name\</th> \<th>Salary\</th>                            \</tr>        \</tfoot>    \</table>    \</div> |
| --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

**Step 3:** Apply Datatable functionality on our HTML table.

| 1234567891011 | $( document ).ready(function() {$('#example').dataTable({ "bProcessing": true,                 "sAjaxSource": "response.php", "aoColumns": \[ { mData: 'Empid' } ,                        { mData: 'Name' },                        { mData: 'Salary' }                ]        });  }); |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

**Step 4:** Now we will create `response.php` file and write down below code.

| 1234567891011121314151617 | $data = array( array('Name'=>'parvez', 'Empid'=>11, 'Salary'=>101), array('Name'=>'alam', 'Empid'=>1, 'Salary'=>102), array('Name'=>'phpflow', 'Empid'=>21, 'Salary'=>103) );              $results = array( "sEcho" => 1,        "iTotalRecords" => count($data),        "iTotalDisplayRecords" => count($data),          "aaData"=>$data);/\*while($row = $result->fetch\_array(MYSQLI\_ASSOC)){  $results\["data"]\[] = $row ;}\*/ echo json\_encode($results); |
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |

> Here we have taken constant array instead of MySQL records, I am assuming you will replace this constant array with your MySQL result set.

[  Live Demo](https://www.phpflow.com/demo/datatable/)

[ Download Source Code](https://www.phpflow.com/source_code/simple_datatable_example_code/simple_datatable_example_code.zip)

&#x20;[Php](https://www.phpflow.com/category/php/)  [datatable with ajax](https://www.phpflow.com/tag/datatable-with-ajax/), [datatable with php](https://www.phpflow.com/tag/datatable-with-php/), [DataTables Example](https://www.phpflow.com/tag/datatables-example/), [Server-side Processing with PHP](https://www.phpflow.com/tag/server-side-processing-with-php/).  [permalink](https://www.phpflow.com/php/datatables-example-server-side-processing-with-php/).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learnphp.gitbook.io/learnphp/datatables-example-server-side-processing-with-php.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
