What are some strategies for improving the output logic in PHP to handle different states of SQL query results effectively?

When handling different states of SQL query results in PHP, it is important to use conditional statements to check for specific conditions such as empty results, errors, or successful queries. By implementing appropriate logic based on these conditions, you can effectively handle and display the output to the user.

// Example code snippet for handling different states of SQL query results

// Perform SQL query
$query = "SELECT * FROM users";
$result = mysqli_query($connection, $query);

// Check if query was successful
if ($result) {
    // Check if there are results
    if (mysqli_num_rows($result) > 0) {
        // Fetch and display results
        while ($row = mysqli_fetch_assoc($result)) {
            echo "User ID: " . $row['id'] . " - Name: " . $row['name'] . "<br>";
        }
    } else {
        echo "No results found.";
    }
} else {
    // Display error message
    echo "Error: " . mysqli_error($connection);
}