What is the best practice for displaying a message when no data is found in a PHP query result?
When a PHP query result returns no data, it is important to display a message to inform the user that no results were found. This can be achieved by checking the number of rows returned by the query and displaying a message if the count is zero. The message should be clear and user-friendly to communicate the absence of data effectively.
// Execute the query
$result = mysqli_query($connection, "SELECT * FROM table WHERE condition");
// Check if any rows were returned
if(mysqli_num_rows($result) == 0) {
echo "No data found.";
} else {
// Display the data
while($row = mysqli_fetch_assoc($result)) {
// Display data here
}
}