First check to see if $result is false after the query is executed, and if so die() or otherwise echo/log the error and the query to debug it. If not false, check to see if mysql_num_rows() is > 0. If not, again log/repot the query string to see if it looks correct, and test it by directly entering it into the MySQL command line interface to verify that your query/logic is correct. Ultimately you'll want some sort of defensive coding approach like:
$result = mysql_query($query);
if($result)
{
if(mysql_num_rows($result))
{
$message = mysql_result($result, 0);
if(mail($to, $subject, $message, $headers))
{
// success message here
}
else
{
echo "Sorry, there was a problem sending the email.";
}
}
else
{
error_log("No row returned: $query");
echo "Sorry, no matching data was found, email not sent.";
}
}
else
{
error_log(mysql_error()."\n".$query);
echo "Sorry, there was a problem with the database--email not sent.";
}