How can the ORDER BY clause be properly used in a SQL query to display database results in descending order based on a specific column?
To display database results in descending order based on a specific column in a SQL query, you can use the ORDER BY clause followed by the column name and the keyword DESC for descending order. This will arrange the results in reverse order based on the specified column.
$query = "SELECT * FROM table_name ORDER BY column_name DESC";
$result = mysqli_query($connection, $query);
// Loop through the results
while($row = mysqli_fetch_assoc($result)) {
// Output the results
echo $row['column_name'] . "<br>";
}
Related Questions
- What are potential pitfalls when fetching and displaying data from a MySQL database in PHP?
- What are the differences between using <meta http-equiv="Refresh" content="5; url=http://example.com"> and PHP header("Refresh: 10; url=http://www.php.net") for redirecting after a certain time?
- What are the potential pitfalls of using register_globals in PHP form scripts for automatic email sending, and why should it be avoided?