Hi,

I have a routine script, that runs every minuite. Part of the routine is to copy the contents of an Access database to a mysql database. I use OLEDB as the COM interface.

Now, as the access db is live it can be prone to locking and other strange behavior which can cause OLEDB to error, hence, causing PHP to Fatal Error.

Anyway, this is not the problem. What i need to do, is restart the script automatically, if a fatal error occours.

Anyonehave any ideas how i can do this?

Thanks

ChaosT :eek:

    if your using php5 you might be able to use exceptions to handle the error and rerun the script.

      You can't really recover from fatal errors since they are, well, fatal...and halt script execution. Exceptions are really there for handling application level errors (e.g. passing the wrong data type to a method), not system level ones (e.g. calling an undefined function).

      If it gets run every minute then it is getting restarted - is that not good enough? If not then can you test the database connection before working with it (and perhaps sleep( 5 ) for each unsucessful test)?

        5 days later

        Shrike, Thorpe, thanks;

        The script takes approximately 3 seconds to run and does tons of stuff - when busy, the script can go up to 1min.

        I use meta refresh rather than a scheduler, as (how can i put this) if the script is only taking 3 seconds to run, it really needs to run every 3 seconds. If it takes 10 seconds, then it needs to run every 10 seconds etc.

        So, the refresh is controlled within the script - which is no good when the fatal error occurs and terminates the script.

        If i can't bypass the fatal error then, how would i go about 'testing' an OLEDB database connection for 'unrecognised database format' errors? I don't even know that this is possible...

        🙁

          OK,

          following thorpe's advice, here's what i am trying:

          function error_handler () {
             print "ERROR";
             print "<meta http-equiv=\"refresh\" content=\"2;url=http://xxx.xxx.xxx.xxx/error.php\">";
          
          }
          
          set_error_handler ('error_handler'); 
          
          

          Guess i will see what happens - unless anyone can see anything glaringly wrong with this method.... :glare:

            That's not really bypassing the error as any errors will still occur (set_error_handler() is provided to allow you to roll your own error messages). But it should restart the script which is the desired effect.

              Thanks Shrike - seems to be working okay so far.... 🙂

                Hmmmmm - not working. :bemused:

                Seems as though the Fatal Error is not being picked up with the error handler (although when i create an error, it is picked up).

                Is it possible that the Fatal Error being reported is not generated by PHP and hence cannot be handled by php? Does the set_error_handler function pick up on 'COM' errors?

                (will capture and post error next time it happens)

                  I thought you had only posted a small part of the function 🙂

                  Have a look at the manual page - your function should accept a set number of parameters, one of which defines the level of error which occured and react accordingly.

                    Geezzzeee - getting nowhere fast here.

                    I have set up the error handler, and it now definately works - i have tested it, but just in case anyone wants to check:

                    // Decide what errors to report:
                    error_reporting (E_ERROR | E_USER_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_WARNING | E_NOTICE);
                    
                    
                    // Webmaster email and error page url:
                    $Wmemail = 'xxx@xxxxx.com';
                    $ErrorPage = 'http://www.yoursite.com/error.html';
                    
                    // This function will check the error type:
                    function MailErrorHandler($errno, $errstr, $errfile='?', $errline= '?')  {
                             global $Wmemail, $ErrorPage;
                    
                         if (($errno & error_reporting()) == 0)
                         return;
                    
                         $err = '';
                         switch($errno) {
                                        case E_ERROR: $err = 'FATAL'; break;
                                        case E_USER_ERROR: $err = 'FATAL'; break;
                                        case E_CORE_ERROR: $err = 'FATAL'; break;
                                        case E_COMPILE_ERROR: $err = 'FATAL'; break;
                                        case E_WARNING: $err = 'ERROR'; break;
                                        case E_NOTICE: return;
                         }
                    
                         // Send the error details via email:
                         mail($Wmemail,
                         "PHP: $errfile, $errline",
                         "$errfile, Line $errlinen$err($errno)n$errstr");
                    
                         // Redirect to the error page:
                         print "<meta http-equiv=\"refresh\" content=\"0;url=http://error page\">";
                         die();
                    }
                    
                    set_error_handler('MailErrorHandler');
                    

                    Now this handles all errors, but ones like this:

                    Fatal error: Uncaught exception 'com_exception' with message 'Source: Microsoft JET Database Engine
                    Description: Not a valid password.' in c:\Inetpub\wwwroot\script.php:22 Stack trace: #0 c:\Inetpub\wwwroot\script.php(22): com->Open('Provider=Micros...') #1 c:\Inetpub\wwwroot\script.php(18): require('c:\Inetpub\wwwr...') #2 {main} thrown in c:\Inetpub\wwwroot\script.php on line 22

                    Here is the code i use to connect:

                    $conn = new COM("ADODB.Connection") or die("Cannot start ADO");
                    
                    $conn->Open("Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Jet OLEDB:Database Password=xxxxxxxxx;Data Source=D:\\sheet1.mdb;");
                    if ($conn->State == 1) { } else {die("TEST CONNECTION FAILED"); }
                    

                    Now, problem is, if the connection does not connect, php does not even get to the next line if ($con->State == 1 etc...

                    Any ideas on how i can either:
                    a) handle the error in a different way or
                    b) properly test the OLEDB connection to stop the error in the first place....

                    Yours,

                    ::mega confused:: ChaosT

                      Write a Reply...