PHP MySQL Using the ORDER BY Clause

PHP Tutorial

 PHP MySQL Using the ORDER BY Clause

In PHP MySQL Using the ORDER BY Clause allows you to sort query results in ascending (ASC) or descending (DESC) order. You can use it with SELECT statements in PHP.


 Using mysqli (Procedural)


 

  • ASC → ascending (default), DESC → descending

 Using mysqli Object-Oriented


 

  •  Sorts users by name in descending order.

 Using PDO


 

  •  PDO supports the same ORDER BY syntax as MySQL.

 Using Multiple Columns

You can order by more than one column:


 

  • First sorts by age ascending

  • Then sorts by name descending if ages are equal


 Key Points

  1. ORDER BY sorts the query results.

  2. Use ASC for ascending (default) and DESC for descending order.

  3. Multiple columns can be used for secondary sorting.

  4. Works with mysqli procedural, object-oriented, and PDO.

  5. Combine with WHERE to filter results before sorting.

You may also like...