How can the script be modified to display a more informative error message when no matching records are found in the database?

The issue of displaying a more informative error message when no matching records are found in the database can be solved by adding a conditional check after the query execution to see if any rows were returned. If no rows are found, then display a custom error message to inform the user.

// Execute the query
$result = mysqli_query($conn, $query);

// Check if any rows were returned
if(mysqli_num_rows($result) == 0) {
    echo "No matching records found in the database.";
} else {
    // Fetch and display the records
    while($row = mysqli_fetch_assoc($result)) {
        // Display the records
    }
}