What are the best practices for handling result sets in PHP to prevent data loss?

When handling result sets in PHP, it is important to properly iterate through the data and store it securely to prevent data loss. One common practice is to use a loop to fetch each row from the result set and store it in an array or object. This ensures that all data is captured and can be safely manipulated without the risk of losing any information.

// Example of handling result sets in PHP to prevent data loss
$result = mysqli_query($conn, "SELECT * FROM users");

if (mysqli_num_rows($result) > 0) {
    $users = array();

    while ($row = mysqli_fetch_assoc($result)) {
        $users[] = $row;
    }

    // Process or display the data stored in the $users array
} else {
    echo "No users found.";
}