How can one check if a query result is empty before attempting to fetch data in PHP?

To check if a query result is empty before attempting to fetch data in PHP, you can use the `rowCount()` method to determine the number of rows returned by the query. If the row count is greater than 0, it means there are results to fetch. You can then proceed to fetch the data using methods like `fetch()` or `fetchAll()`. If the row count is 0, it means the query returned no results, and you should handle this case accordingly.

// Assuming $stmt is your PDO statement object
$stmt->execute();
if ($stmt->rowCount() > 0) {
    // Fetch data from the query result
    $result = $stmt->fetch();
    // Process the fetched data
} else {
    // Handle case where query result is empty
    echo "No results found.";
}