nonorignal
nonorignal

Reputation: 3

Class 'MyDatabaseException' not found in /public_html/Connections/pdo.php on line 17

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:

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

Upvotes: 0

Views: 396

Answers (1)

Alex Howansky
Alex Howansky

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

Related Questions