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

  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.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *