PHP MySQL Using the ORDER BY Clause
📊 PHP MySQL Using the ORDER BY Clause
The ORDER BY clause in MySQL allows you to sort query results in ascending (ASC) or descending (DESC) order. You can use it with SELECT statements in PHP.
1️⃣ Using mysqli (Procedural)
✅ ASC → ascending (default), DESC → descending
2️⃣ Using mysqli Object-Oriented
✅ Sorts users by name in descending order.
3️⃣ Using PDO
✅ PDO supports the same ORDER BY syntax as MySQL.
4️⃣ 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
5️⃣ Key Points
-
ORDER BY sorts the query results.
-
Use ASC for ascending (default) and DESC for descending order.
-
Multiple columns can be used for secondary sorting.
-
Works with mysqli procedural, object-oriented, and PDO.
-
Combine with WHERE to filter results before sorting.
