How can the issue of empty results from a MySQL query in PHP be addressed and resolved when comparing user login credentials?

Issue: The issue of empty results from a MySQL query in PHP when comparing user login credentials can be addressed by checking if the query returned any rows before attempting to access the results. This can prevent errors when trying to fetch data from an empty result set.

// Check if the query returned any rows
if($result->num_rows > 0){
    // Fetch the row and compare user credentials
    $row = $result->fetch_assoc();
    if($row['username'] == $username && $row['password'] == $password){
        // Login successful
    } else {
        // Login failed
    }
} else {
    // No matching user found
}