How can the use of LIMIT in a SQL query be a better alternative to limiting the iterations of a while loop in PHP?

Using the LIMIT clause in a SQL query can be a better alternative to limiting the iterations of a while loop in PHP because it allows the database to handle the filtering and limiting of results, which can be more efficient and optimized. This can reduce the amount of data transferred between the database and the PHP script, resulting in better performance.

// Using LIMIT in SQL query to limit the number of results returned
$sql = "SELECT * FROM table_name LIMIT 10";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Column1: " . $row["column1"]. " - Column2: " . $row["column2"]. "<br>";
    }
} else {
    echo "0 results";
}