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:
- PHP message: PHP Stack trace:
- PHP message: PHP 1. {main}() /public_html/jpa.php:0
- PHP message: PHP 2. require_once() /public_html/jpa.php:9
- PHP message: PHP 3. require_once() /public_html/common/japan.php:9
- PHP message: PHP 4. PDO->__construct('mysql:host=localhost;dbname=xxx;charset=utf8', 'xxx', 'xxx') /public_html/Connections/pdo.php:10
- PHP message: PHP Fatal error: Class 'MyDatabaseException' not found in /public_html/Connections/pdo.php on line 17
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.