I would suggest you move your 'while' loop out of MAIL and
put the data into a $message variable and print MAIL "$message\n";
If $message contains something, send the email.
On a side: I hope that the $dsn, $user_name, and $password data
are not real.
Comment on Re: Testing to see if a MySQL query is true