PHP MySql ORDER BY

PHP MySql ORDER BY clause tutorial will explain you How to SORT data in MySQL with PHP? with Examples. To sort MySQL query result ORDER BY (ASC or DESC) clause is used.

You can order MySQL result using “ORDER BY”

  1. ORDER BY column_name ASC, orders in ascending manner.
  2. ORDER BY column_name DESC, orders in descending manner.
  3. ORDE BY RAND(), orders in random manner.

Ascending is a default order for “ORDER BY” clause.

Syntax for PHP MySql ORDER BY clause:

Example of PHP MySql ORDER BY Clause:

Output:

26 sam
27 thomas
30 mark
33 john

Order by two columns

We can also sort multiple columns in different directions as given in the below MySQL example.

Multiple-column sorts can be ascending or descending, but DESC must be specified explicitly on each column to perform a fully descending sort as shown below:

Example on Order by two columns:

SHARE
Previous articlePHP MySql WHERE clause
Next articlePHP MySQL UPDATE Query
I am a young Information Systems Security Engineer(CEH). I had started with penetration testing when i am 17 years old. It all started with Kali linux and Metasploit.In free time i will play with Security holes/Bug bounty/Pentesting and Hacking !!!