Server Side Processing DataTable with PHP and MySQL

Server Side Processing DataTable with PHP and MySQL

Server Side Processing DataTable with PHP and MySQL

DataTables is a jQuery library used to show the entire list of records in an HTML table. It combines some features like key search, pagination list, data sorting, filter and etc. In DataTables we can limit the number of records to be presented per page.

DataTables gives both client-side and server-side processing. In this post, we are going to utilize the database results by using DataTables server-side processing. A PHP file used to calls requested columns and returns array of results. This resultant array is encoded to the JSON format which will send the response to the DataTables AJAX script.

DataTable Defining Code

The following code uses to define HTML code for the DataTable. This code has needed CSS and JavaScript library includes a top of the page. The table HTML tag has an id attribute which is used to define and set the table as a DataTable.

<!-- DataTable CSS -->
<link rel="stylesheet" type="text/css" href=""/>

<!-- DataTable Script -->
<script type="text/javascript" src=""></script>

and HTML Table details

<table id="post_list" class="dataTable" width="100%" cellspacing="0">
			<th>Post Name</th>


DataTable Initialize using jQuery script

The following code shows the jQuery DataTable Initialize by setting the needed DataTable property key features. In this code, the serverSide property is set as true and the server side file path is specified for the ajax property key. These keys are used to set the server side processing for the DataTable.

	"bProcessing": true,
 	"serverSide": true,
        url :"post_list.php",
        type: "POST",
        error: function(){

Server-Side Processing script

The following PHP code is provided the data set details as JSON format. Here the data will get from MySql table with help of SELECT with ORDER and LIMIT keys. It contains column name and respective values as an array.

	$params = $columns = $totalRecords = $data = array();

	$params = $_REQUEST;

	$columns = array(
		0 => 'post_id',
		1 => 'post_title', 
		2 => 'post_desc'

	$where_condition = $sqlTot = $sqlRec = "";

	if( !empty($params['search']['value']) ) {
		$where_condition .=	" WHERE ";
		$where_condition .= " ( post_title LIKE '%".$params['search']['value']."%' ";    
		$where_condition .= " OR post_desc LIKE '%".$params['search']['value']."%' )";

	$sql_query = " SELECT * FROM li_ajax_post_load ";
	$sqlTot .= $sql_query;
	$sqlRec .= $sql_query;
	if(isset($where_condition) && $where_condition != '') {

		$sqlTot .= $where_condition;
		$sqlRec .= $where_condition;

 	$sqlRec .=  " ORDER BY ". $columns[$params['order'][0]['column']]."   ".$params['order'][0]['dir']."  LIMIT ".$params['start']." ,".$params['length']." ";

	$queryTot = mysqli_query($con, $sqlTot) or die("Database Error:". mysqli_error($con));

	$totalRecords = mysqli_num_rows($queryTot);

	$queryRecords = mysqli_query($con, $sqlRec) or die("Error to Get the Post details.");

	while( $row = mysqli_fetch_row($queryRecords) ) { 
		$data[] = $row;

	$json_data = array(
		"draw"            => intval( $params['draw'] ),   
		"recordsTotal"    => intval( $totalRecords ),  
		"recordsFiltered" => intval($totalRecords),
		"data"            => $data

	echo json_encode($json_data);

Tepkiniz nedir?