Added exception handling and error reporting for each segment of code. Added database reconnection code, that reconnects to the MySQL server if an exception is caught during sql query. Added error reporting settings in head of script.