How can SQL queries be optimized to sort results by time in descending order and limit them to a specific number of records in PHP?
To optimize SQL queries to sort results by time in descending order and limit them to a specific number of records in PHP, you can use the ORDER BY clause with the DESC keyword to sort by time in descending order, and the LIMIT clause to restrict the number of records returned. This can help improve the performance of your queries and ensure that only the necessary data is retrieved.
$query = "SELECT * FROM table_name ORDER BY time_column DESC LIMIT 10";
$result = mysqli_query($connection, $query);
// Fetch and display the results
while ($row = mysqli_fetch_assoc($result)) {
// Display or process the data as needed
}