How can PHP be used to handle error messages when a result is not found in the database?
When a result is not found in the database, PHP can handle this situation by checking the return value of the query and displaying a custom error message to the user. This can be achieved by using conditional statements to check if the query returned any results, and if not, displaying an appropriate message.
// Perform a database query
$result = mysqli_query($connection, "SELECT * FROM table WHERE id = 123");
// Check if any results were found
if(mysqli_num_rows($result) == 0) {
echo "No results found in the database.";
} else {
// Process the results
while($row = mysqli_fetch_assoc($result)) {
// Display or process the data
}
}
// Free the result set
mysqli_free_result($result);