Ajax Jquery Column Sorting Results using PHP MySQL

Today, We want to share with you Ajax Jquery Column Sorting Results using PHP MySQL ORDER BY.In this post we will show you Ajax Jquery Column Sort with PHP & MySql, hear for Sort Column (Ascending and Descending) in PHP/MySQL we will give you demo and example for implement.In this post, we will learn about Sorting Results with MySQL ORDER BY Clause in PHP with an example.

Ajax Jquery Column Sorting Results using PHP MySQL ORDER BY

There are the Following The simple About Ajax Jquery Column Sorting Results using PHP MySQL ORDER BY Full Information With Example and source code.

As I will cover this Post with live Working example to develop Column Sorting using PHP and MySQL, so the some major files and Directory structures for this example is following below.

  • index.php

Step 1: Create Database table

simple mysql Run This Query and save Database structures.

members Table in MySQL

  `name` varchar(80) NOT NULL,
  `age` varchar(244) NOT NULL,
  PRIMARY KEY (`id`)


Step 2 : Main HTML Interface

Create index.php file:

This is where I will make a simple HTML form and PHP server side source code for our web application. To make the forms simply all souce code copy and write it into your any text editor Like Notepad++, then save file it as index.php.

<!DOCTYPE html>
  <title>Column Sorting using PHP and MySQL - pakainfo.com</title>
  <link rel="stylesheet" href="cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />

<div class="pakainfo container">
  <h1>Column Sorting using PHP and MySQL - pakainfo.com</h1>

    $hostName = "localhost";
    $username = "atmiya25";
    $password = "jdpakainfo";
    $dbname = "membersmanagae";

    $mysqli = new mysqli($hostName, $username, $password, $dbname);
    $fieldByOrder = !empty($_GET["field_by_sortdata"]) ? $_GET["field_by_sortdata"] : "name";
    $sortdata = !empty($_GET["sortdata"]) ? $_GET["sortdata"] : "asc";
    $sql = "SELECT * FROM members ORDER BY " . $fieldByOrder . " " . $sortdata;
    $result = $mysqli->query($sql);

    $memberNameSort = "asc";
    $ageMemberSort = "asc";

    if($fieldByOrder == "name" && $sortdata == "asc") {
      $memberNameSort = "desc";
    if($fieldByOrder == "age" && $sortdata == "asc") {
      $ageMemberSort = "desc";
<h3>sort table columns using php by clicking header(asc desc)</h3>
<table class="table table-bsortdataed live-tbl">
      <th><a href="?field_by_sortdata=name&sortdata=<?php echo $memberNameSort; ?>">Name</a></th>
      <th><a href="?field_by_sortdata=age&sortdata=<?php echo $ageMemberSort; ?>">Age</a></th>

    while($member_row = mysqli_fetch_assoc($result)){
        <td><?php echo $member_row['name']; ?></td>
        <td><?php echo $member_row['age']; ?></td>

Angular 6 CRUD Operations Application Tutorials

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Ajax Jquery Column Sorting Results using PHP MySQL ORDER BY.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Related FAQ

Here are some more FAQ related to this Article:

  1.   MySQL LEFT Inner and outer where clause joins
  2.   Laravel 6 Allow CORS All requests Middleware Tutorial
  3.   Retrieve Data From MySQL Using PHP
  4.   Hyperlinks not working with jquery mobile
  5.   Dropdown Validation using Angular Example
  6.   PHP MySQL Column Sorting Examples
  7.   SSL forcefully Redirect HTTP to HTTPS Using .htaccess File
  8.   Upload Multiple Images With Image Preview Using jQuery,Ajax And PHP
  9.   Dynamic and Dependent Dropdown list using AngularJS with PHP
  10.   Laravel AJAX Delete Record From MySQL Table