How can a custom text message be displayed when no results are found in a MySQL query in PHP?
If no results are found in a MySQL query in PHP, you can display a custom text message by checking the number of rows returned by the query. If the number of rows is zero, you can echo out the custom message.
// Execute the MySQL query
$result = mysqli_query($conn, "SELECT * FROM table WHERE condition");
// Check if any results are found
if(mysqli_num_rows($result) == 0) {
echo "No results found.";
} else {
// Process the results
while($row = mysqli_fetch_assoc($result)) {
// Display or use the results
}
}
Keywords
Related Questions
- What are the differences in session handling between different browsers when working with PHP code that generates PDF files?
- What steps should be taken when encountering issues with a specific PHP framework, such as the Teamspeak 3 PHP framework?
- How can PHP developers effectively troubleshoot syntax errors in SQL queries?