Don't output wrong errors in the log

* Output the SQLSTATE and the database error info to logs for database errors.
* Avoid comparing with ```==```, always use ```===``` to make sure the result is interpreted correctly.
* Make URL parsing more robust by relying on PHP's ```parse_url()```.

Fixes #49.
Fixes #87.