Working with jQuery DataTables server-side processing using PHP and MySQL
In this PHP Tutorial, I am going to tell you how to use jQuery datatable plugin with Server-side processing with PHP and MySQL.
jQuery DataTables have lots of flexibility built-in by default. For example it provides sorting, pagination, searching functionality that is not so easy in HTML tables.
Using jQuery datatable, you transform your HTML table from classical to DataGridView component.
In this example, I will show you basic example of jQuery datatable to list whole data at once from MySQL database.
If there are large amount of records in your database then it can be a performance issue to get all data from server at once.
In next example, I will show you how to integrate server side pagination, searching and sorting using jQuery DataTables in PHP MySQL.
For this example, I need to obtain the library of jQuery and jQuery DataTables.Step1: Create Employee Table
To start with this example, You should have a table with some records so I will create a new table "employee" in MySQL database by running following query :Step2: Create employee.html file
In this step, I will create "employee.html" file and include the DataTables CSS in the top and in the bottom include JS files and instantiate the DataTable on the table.employee.html Step 3: Create employee.php File
In this step, I will create a "employee.php" file and configure the database and write a simple MySQL select query to get records from employee table.
DataTables expect some attributes in the returned json data and they are :
- iTotalRecords – It should be the total number of records before applying the filters.
- iTotalDisplayRecords – It should be the total number of records after applying the filters.
- sEcho – Must be integer type for security reason. This is an unaltered copy of sEcho sent from the client side.
- aaData – It will return the array of data from the server.