ORDER BY clause is used to sort the database table data in ascending or descending order.

MySQL Query Syntax:

SELECT column_name FROM table_name ORDER BY column_name ASC|DESC 

SELECT .... ORDER BY MySQLi PHP

Suppose we have a table customer and we want to order by customer name look at the following table 

+----+----------+------------------+---------+
| id | name     | city             | Country |
+----+----------+------------------+---------+
|  1 | Rathorji | Mumbai           | India   |
|  2 | Jhone    | Washington, D.C. | USA     |
|  3 | Amit     | Delhi            | India   |
|  4 | Rohit    | Indore           | India   |
+----+----------+------------------+---------+

Select Order by Data With MySQLi

Example: 

<!DOCTYPE html>
<html>
    <head>
        <title>Order By Mysqli</title>
    </head>
    <body>

        <?php
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "tutorial";

      //connection
        $conn = new mysqli($servername, $username, $password, $dbname);
       // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $sql = "SELECT * FROM Customer ORDER BY city";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            echo "<table border='1'><tr><th>Customer Name</th><th>City</th><th>Country</th></tr>";
            // output data of each row
            while ($row = $result->fetch_assoc()) {
                echo "<tr><td>" . $row["name"] . "</td><td>" . $row["city"] . "</td><td>" . $row["Country"] . "</td></tr>";
            }
            echo "</table>";
        } else {
            echo "0 results";
        }

        $conn->close();
        ?>

    </body>
</html>

Database Table structure

-- phpMyAdmin SQL Dump
-- version 5.0.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Dec 31, 2020 at 04:24 AM
-- Server version: 10.4.17-MariaDB
-- PHP Version: 7.3.25

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `tutorial`
--

-- --------------------------------------------------------

--
-- Table structure for table `customer`
--

CREATE TABLE `customer` (
  `id` int(11) NOT NULL,
  `name` varchar(32) NOT NULL,
  `city` varchar(32) NOT NULL,
  `Country` varchar(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Dumping data for table `customer`
--

INSERT INTO `customer` (`id`, `name`, `city`, `Country`) VALUES
(1, 'Rathorji', 'Mumbai', 'India'),
(2, 'Jhone', 'Washington, D.C.', 'USA'),
(3, 'Amit', 'Delhi', 'India'),
(4, 'Rohit', 'Indore', 'India');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `customer`
--
ALTER TABLE `customer`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `customer`
--
ALTER TABLE `customer`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


Output: