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?
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.