Jump to content

Error handling when using WireDatabasePDO?


AAD Web Team
 Share

Recommended Posts

Is it possible to connect to a non-ProcessWire database using WireDatabasePDO, and have error handling, so if the database is unavailable then the page can recover gracefully? I tried:

try {
	$db = new WireDatabasePDO([ /* dsn, user, pass */ ]);
} catch (\Exception $e) {
	/* Code to handle the problem gracefully */
}

This doesn’t work – it seems that the exception is dealt with by ProcessWire before my code sees it. Is it better just to use PHP’s native PDO object directly? Something such as:

try {
	$dbh = new \PDO($dsn, $user, $password);
} catch (\PDOException $e) {
	/* Code to handle the problem */
}

 

Link to comment
Share on other sites

WireDatabasePDO's constructur doesn't connect to the database yet. This is either implicitly done when the query or exec(ute) methods are called, or explicitly by invoking $db->pdo(), which will return the underlying plain PDO object.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...