How can PHP handle varying query results with different numbers of columns and rows when displaying them on a webpage?

When handling varying query results with different numbers of columns and rows in PHP, we can use functions like `mysqli_num_rows()` and `mysqli_num_fields()` to determine the number of rows and columns in the result set. We can then dynamically generate HTML tables or lists based on these numbers to display the data on a webpage.

// Assuming $result is the result set from a query

if ($result) {
    $num_rows = mysqli_num_rows($result);
    $num_cols = mysqli_num_fields($result);

    echo "<table>";
    for ($i = 0; $i < $num_rows; $i++) {
        $row = mysqli_fetch_array($result);
        echo "<tr>";
        for ($j = 0; $j < $num_cols; $j++) {
            echo "<td>" . $row[$j] . "</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "No results found.";
}