Capture E_Warnings

Is it possible to capture all error, warnings and notice issues on a page, and handle them myself?

For example, currently I have an issue with my site saying:-
Warning: mysqli_connect(): (HY000/1045): Access denied for user

I know what the issue is (I dont have MySQL installed on my home laptop), however I want to be able to capture this warning, store it, and then display a generic error message to the user on a new page, so redirect the user to displayIssue.php .

I thought I could just simply use:-
<?php
    function customError($errno, $errstr, $errfile, $errline) {
        header('Location: ../errorHappened.php');
        die();
    }

    set_error_handler("customError");
?>

Open in new window


However after a little bit of reading I discovered you cant use set_error_handler for warnings :-(

Anyone got any other clues on how to prevent them displaying to a user and handle them myself?

Thank you
tonelm54Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aboo_sCommented:
you can do something like this:

if(!mysqli_connect())   header('Location: ../errorHappened.php');
0
Dave BaldwinFixer of ProblemsCommented:
This page http://php.net/manual/en/mysqli.construct.php shows how to write the proper code to get the error.  Note that this is Not a PHP error but an error coming from the 'mysqli' driver about the MySQL connection.  PHP 'error_reporting' will not be triggered by this.
0
Ray PaseurCommented:
It's a rather advanced concept, so set up your test cases carefully!  You can tell PHP to turn errors into exceptions.
http://php.net/manual/en/class.errorexception.php

If you use trigger_error() with E_USER_WARNING when you get a MySQL failure, you can probably achieve what you're looking for.  The triggered error will be converted to an Exception and you can have an exception handler that will log the information.
0
Ray PaseurCommented:
After a bit of experimenting, this seems to make sense.  It interferes with the minimum number of warning conditions and seems to isolate the MySQLi warnings pretty well.  The error_log() will let you keep track of what / when the silent warnings were occurring.

<?php // demo/temp_tonelm54.php

/**
 * See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28624445.html
 *
 * Catch and suppress Warnings from MySQLI
 *
 * Ref http://php.net/manual/en/errorfunc.constants.php
 * Ref http://php.net/manual/en/function.set-error-handler.php
 */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);


/**
 * Catch and suppress Warnings from Failed MySQLI Attempts
 */
function mysqli_handler($errno, $errstr, $errfile, $errline)
{
    // WHAT SHOULD BE IGNORED? mysqli::query(), mysqli::mysqli
    $signal = 'mysqli::';
    $siglen = strlen($signal);
    if (substr($errstr,0,$siglen) == $signal)
    {
        $logmsg = "$errstr on line $errline in $errfile";
        error_log($logmsg);
        return TRUE;
    }
    return FALSE;
}
set_error_handler("mysqli_handler", E_WARNING);


// DATABASE CONNECTION AND SELECTION VARIABLES
$db_host = "localhost";
$db_name = "??";
$db_user = "??";
$db_word = "??";


// TRY TO OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);


// PROVE THAT THE CONNECTION FAILED
$sql = "SELECT 1+1";
$res = $mysqli->query($sql);
var_dump($res);


// PROVE THAT OTHER WARNINGS ARE NOT SUPPRESSED
mysql_connect('localhost', 'mysql_user', 'mysql_password');

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.