setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $db; } catch (PDOException $ex) { handle_error("Can not connect to the database. Please try again later.", $ex); } } /** * Prints out a plain text 400 error message given $msg. If given a second (optional) argument as * an PDOException, prints details about the cause of the exception. * @param $msg {string} - Plain text 400 message to output * @param $ex {PDOException} - (optional) Exception object with additional exception details to print */ function handle_error($msg, $ex=NULL) { header("HTTP/1.1 400 Invalid Request"); header("Content-type: text/plain"); print ("{$msg}\n"); if ($ex) { print ("Error details: $ex \n"); } } ?>