0

I am having a strange situation.

When a class instance is passed to a method & the class instance has not been set, the server throws a 500 error, instead of catching the error.

This happends during testing 99% of the time, but I want to make sure a user never sees it for that 1%

BTW : I have already checked my .htaccess and apache2 files <= they are correct

What do i need to change in my php.ini file or something similar to avoid a 500 error everytime this happens??

Here is some sample code :

<?php

function querysomething ($connection_to_mysql, $query) {
  try {
    $query = $connection_to_mysql->prepare("SELECT * FROM table WHERE name=?");
    $query->bind_param("s", $name);
    $execute = $query->execute();
    //etc...
    if (!$execute) {
      throw new Exception ("some error");
    }
  } catch (Exception $e) {
    //some error handling
  }
}

?>

If the $connection_to_mysql object is not instantiated, the server throws a 500 error

Do I need to check if the $connection_to_mysql actually exists everytime prior to query?

mlishn
  • 1,689
  • 14
  • 19
  • 500 in this case is usually some runtime error in PHP. Turn the ini setting `display_errors` on and you should be able to see what the actual error is. – Explosion Pills Aug 08 '12 at 16:09
  • I have like this (when testing) : `error_reporting(E_ALL); ini_set('display_errors','On');` I'm just wondering if this could happen even after I fix the errors. I dont want my users to get a `500` – mlishn Aug 08 '12 at 16:12
  • then what is the actual error message? – Explosion Pills Aug 08 '12 at 16:21
  • I'm not near my workspace, but its usually a fatal error with mysqli object not found or someother object not found. When these errors happen because of a typo or database connection not created or whatnot, I handle them right there. I am worried about : *If the database connection is lost and the object is not instantiated, then passed to a function, it will throw the `500`.* I need a way to somehow handle `500` errors (redirect page or anything -- I've tried in `apache` but it doesn't redirect) – mlishn Aug 08 '12 at 16:29

2 Answers2

3

Use set_error_handler/set_exception_handler to display a php fatal error-specific page.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • I did some research & that doesn't handle `(E_ERROR)`s so its advised to use `register_shutdown_functions`. I included an answer with the link below. Thanks for your help, I will write something up with your `set_error_handler/set_exception_handler` and the link below – mlishn Aug 08 '12 at 17:04
0

After doing some additional reading on catching Fatal Errors, I can across this : How do I catch a PHP Fatal Error

Instead of using set_error_handler & set_exception_handler, to catch all errors and process afterwords use register_shutdown_functions.

This would allow you to redirect to a page (ex : 500.php or whatever), log the error, & send an email if desired.

Community
  • 1
  • 1
mlishn
  • 1,689
  • 14
  • 19