I am using a software that doesn't have a config file or error page file of any sort. I want to have a custom error page in case there's no connection to the database. What are my options? Can I create is mysqli connet with a die in it in the index file and then add a custom error page to it? Will it work?
Sure? Just do a redirect, or load something else than the standard index-file (via include or require) in case of an error.
My problem is everything in that script is encoded via ionCube, so I do not know what is what there. I actually emailed the guys that sold me the script to see if they can assist me in that too. My question to you is is it possible that if I have my own mysqli connect with a custom error page that it can go to an error whereas the whole script will be still running fine. In other words, can mysqli connects to the same database give errors (fail to connect) independently? Or does a fail to connect happen to all of them at once?
You should handle the error when you try a connection to the DB. You can handle it by exception. Catch if there is an error and send a message. try { connection } catch(Exception $e) { echo 'Message: ' .$e->getMessage(); } Code (markup):
While intermittent errors _can_ happen, due to server errors, connection errors, server setup and dozens of other reasons. But no, usually, if you have a SQL-error it will be site-wide, not just one script, or part of a script. In that case it will most likely be a config error somewhere, in the failing script.
If you can't change the source code because of ionCube, i think you could do the next thing. In the .htaccess redirect all the traffic to one new php file, in that new php file include the website index and previously check if you can connect to the database.
I think your time would be far better vested in sorting out the issue that is causing your mysql server to become unavailable so often that you are seeking to create a custom error page for it.
I never said it becomes unavailable often. I said "in case" it becomes unavailable. I like having custom error pages that look like the actual site. I'll probably do what @PoPSiCLe suggested. Will include an error page via an include. It's not a priority at the moment, just wanted to know what my options were.
You can try setting a custom error handler like this : function error_handler($errno, $errstr, $errfile, $errline) { switch ($errno) { case E_NOTICE: case E_USER_NOTICE: $error = 'Notice'; break; case E_WARNING: case E_USER_WARNING: $error = 'Warning'; break; case E_ERROR: case E_USER_ERROR: $error = 'Fatal Error'; break; default: $error = 'Unknown'; break; } /* Custom Action Here */ return true; } // Error Handler set_error_handler('error_handler'); Code (markup):