What are some common pitfalls to avoid when using ORDER BY in SQL queries within PHP scripts, especially when sorting by multiple columns?

When using ORDER BY in SQL queries within PHP scripts to sort by multiple columns, a common pitfall to avoid is not specifying the correct order for each column. To solve this issue, always specify the sorting order (ASC for ascending or DESC for descending) for each column being sorted. This ensures that the results are sorted correctly based on the desired criteria.

// Example SQL query with ORDER BY sorting by multiple columns
$sql = "SELECT * FROM table_name ORDER BY column1 ASC, column2 DESC";
$result = mysqli_query($conn, $sql);

// Fetch and display the sorted results
while($row = mysqli_fetch_assoc($result)) {
    echo $row['column1'] . " - " . $row['column2'] . "<br>";
}