Query MySQL Database, check, log, and mail