What strategies can be implemented to handle cases where a query may return false or no results in PHP, especially when fetching data from a database?

When a query may return false or no results in PHP, it is important to check the result before trying to access it. One common strategy is to use conditional statements to check if the result is valid before proceeding with any operations on it. This can help prevent errors and handle cases where no data is returned from the database.

// Perform a query to fetch data from the database
$result = mysqli_query($connection, "SELECT * FROM table WHERE condition");

// Check if the query was successful and if results were returned
if ($result && mysqli_num_rows($result) > 0) {
    // Fetch and process the data
    while ($row = mysqli_fetch_assoc($result)) {
        // Process each row of data
    }
} else {
    // Handle case where no results were returned
    echo "No results found.";
}