Reputation: 3
I'm not new to php but I'm unfamiliar with exceptions and OOP.
The problem here is that sometimes mysql doesn't load or loads after the web server. So I'm getting this fatal error:
Here is my code, copied directly from the PHP page.
<?php
try
{
$db = new PDO("mysql:host={$hostname_data_connect};dbname={$database_data_connect};charset=utf8", "{$username_data_connect}", "{$password_data_connect}");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch( PDOException $Exception )
{
// Note The Typecast To An Integer!
throw new MyDatabaseException( $Exception->getMessage( ) , (int)$Exception->getCode( ) );
}
What I would like to achieve is to return a 503 maintenance message or redirect the user to a custom 503 maintenance message page instead of throwing a fatal exception. The time mysql finishes loading.
I have no idea how to do that. Please help.
Or perform a php check to test whether mysqld is up and running before executing the code above. Again I have no idea how to do that.
A snippet of code would be greatly appreciated. Dealing with that error for 3 years now.
Upvotes: 0
Views: 396
Reputation: 53563
MyDatabaseException
is not a pre-defined PHP class, it's just an example name of something that you might create yourself, like $myString
or myFunction()
. But you haven't created any such class. Instead of just re-throwing the exception, perform your desired action in the catch block:
try {
// some db stuff
} catch PDOException($e) {
// maybe dynamically generate a 503 page
header('HTTP/1.1 503 Service Unavailable');
// output page content here
exit();
// or redirect to an existing one
header('Location: http://your.domain/your/503/page');
exit;
}
Upvotes: 1