How can one handle cases where data records are not found in a database while looping through them in PHP?

When looping through data records in a database in PHP, it is important to check if any records are found before attempting to process them. One way to handle cases where data records are not found is to use an if statement to check if the result set is empty before entering the loop. If the result set is empty, you can display a message or take appropriate action instead of trying to loop through non-existent data records.

// Assume $result is the result set from a database query

if(mysqli_num_rows($result) > 0) {
    // Loop through the data records
    while($row = mysqli_fetch_assoc($result)) {
        // Process each data record
    }
} else {
    echo "No records found in the database.";
}