How can the problem of losing data when using mysqli_num_rows after mysqli_fetch_assoc be avoided?

When using mysqli_num_rows after mysqli_fetch_assoc, the problem of losing data can be avoided by storing the result of mysqli_fetch_assoc in a variable and then using mysqli_num_rows on the original result set before fetching any rows. This ensures that the data is not lost when calling mysqli_fetch_assoc.

$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
        // process data
    }
}