How can the ORDER BY clause in MySQL be used to sort data by multiple columns in PHP?
To sort data by multiple columns in MySQL using the ORDER BY clause in PHP, you can simply list the columns you want to sort by separated by commas. This will first sort the data by the first column specified, and then by the second column if there are duplicates in the first column.
$query = "SELECT * FROM table_name ORDER BY column1, column2";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)) {
// Output or process the sorted data here
}
Keywords
Related Questions
- How important is it to include correct headers, such as Return-path and charset, when sending emails using PHP?
- What best practices can be implemented to ensure the security and integrity of form data submission in PHP applications?
- How can the use of register_globals affect the functionality of the PHP code?