What are some best practices for sorting and limiting results in PHP when querying a database?

When querying a database in PHP, it is important to properly sort and limit the results to improve performance and user experience. To achieve this, you can use the ORDER BY clause to sort the results based on a specific column and the LIMIT clause to restrict the number of rows returned.

// Sorting and limiting results in PHP query
$query = "SELECT * FROM table_name ORDER BY column_name LIMIT 10";
$result = mysqli_query($connection, $query);

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