Closed Thread
Results 1 to 9 of 9




  

Thread: php set_error_handler

      
  1. #1
    Jedi Master Paramiliar is on a distinguished road Paramiliar's Avatar
    Join Date
    July 24, 2007
    Location
    West Midlands
    Posts
    105
    Rep Power
    5
    Feedback Score
    0

    Question php set_error_handler

    Hey guys, been looking at the set_error_handler function in php and while it will do what I want it to do i want to be able to pass more information to it.

    for example if an sql query doesn't run correct i would then die and call the error handler but I want to be able to pass the query to the function.

    using global wouldn't work as the variable that is used to house the query is dynamic (so sometimes it is $query othertimes it is $query_graph and with over 400 files uniforming them is not an option)

    so any ideas guys I thought of calling another function which then calls the trigger_error() function but i'm thinking it would then get the line number wrong and the file wrong as well.


    AAAAHHHHHHHH:scry::scry::scry::scry:

  2. #2
    Idolized By All noppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nice noppid's Avatar
    Join Date
    October 12, 2006
    Location
    Florida
    Posts
    487
    Rep Power
    6
    Feedback Score
    0

    Default

    That's why many apps use a wrapper to access certain functions such as MySQL calls. If the calls were wrapped, you could include the error code in your functions with very little code needing to be added. That addresses the problem of adding the code.

    As for grabbing "more" and passing it somewhere... PHP: set_error_handler - Manual Look at the examples there. One is very good.
    Free vBulletin hacks and vBulletin Support.
    Talk to bikers around the world about motorcycles.
    Clogging an american dance step.

  3. #3
    Jedi Master Paramiliar is on a distinguished road Paramiliar's Avatar
    Join Date
    July 24, 2007
    Location
    West Midlands
    Posts
    105
    Rep Power
    5
    Feedback Score
    0

    Default

    hey again noppid,
    I have been through that file with a fine comb and cant see how I can pass over the query. the trigger_error is limited to 1024 characters and a lot of the queries we run are well over that heck just one of the maths bit is more than that lol

    i'm guessing your reffering to using a class to handle the sql, I had thought about that but its not just me that does the coding on the files there are 4 other people and I know from experience they wont migrate to an sql class

  4. #4
    Idolized By All noppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nice noppid's Avatar
    Join Date
    October 12, 2006
    Location
    Florida
    Posts
    487
    Rep Power
    6
    Feedback Score
    0

    Default

    To convert mysql calls to a function is a really easy global search and replace for many apps to call one function. So it's not a daunting task at all for a developer.

    You really don't need a class I don't think, just a function. You are passing the query to a function that will call mysql after you set the error handler. If it's not a fatal call which the handler won't handle, which shouldn't be in a production app anyway, the error handler does it's thing. It's thing is a function you code.

    Error handler is dealing with the php generated error from the failed function call. Not the call to the function itself.

    I hope I'm not confused on what you mean by the 1024 limit.
    Free vBulletin hacks and vBulletin Support.
    Talk to bikers around the world about motorcycles.
    Clogging an american dance step.

  5. #5
    Jedi Master Paramiliar is on a distinguished road Paramiliar's Avatar
    Join Date
    July 24, 2007
    Location
    West Midlands
    Posts
    105
    Rep Power
    5
    Feedback Score
    0

    Default

    Good news I have worked it out, this is the code I have

    PHP Code:
    function myErrorHandler($errno$errstr$errfile$errline){
        switch (
    $errno) {
        case 
    E_USER_ERROR:
            if (
    $errstr == "(SQL)"){
                
    // handling an sql error
                
    echo "<b>SQL Error</b> [$errno] " SQLMESSAGE "<br />\n";
                echo 
    "Query : " SQLQUERY "<br />\n";
                echo 
    "On line " SQLERRORLINE " in file " SQLERRORFILE " ";
                echo 
    ", PHP " PHP_VERSION " (" PHP_OS ")<br />\n";
                echo 
    "Aborting...<br />\n";
            } else {
                echo 
    "<b>My ERROR</b> [$errno$errstr<br />\n";
                echo 
    "  Fatal error on line $errline in file $errfile";
                echo 
    ", PHP " PHP_VERSION " (" PHP_OS ")<br />\n";
                echo 
    "Aborting...<br />\n";
            }
            exit(
    1);
            break;

        case 
    E_USER_WARNING:
        case 
    E_USER_NOTICE:
        }
        
    /* Don't execute PHP internal error handler */
        
    return true;
    }

    // function to test the error handling

    function sqlerrorhandler($ERROR$QUERY$PHPFILE$LINE){
        
    define("SQLQUERY"$QUERY);
        
    define("SQLMESSAGE"$ERROR);
        
    define("SQLERRORLINE"$LINE);
        
    define("SQLERRORFILE"$PHPFILE);
        
    trigger_error("(SQL)"E_USER_ERROR);
    }

    set_error_handler("myErrorHandler");

    // trigger an sql error
    $query "SELECT * FROM tbl LIMIT 1";
    $sql = @mysql_query($query)
        or die(
    sqlerrorhandler("(".mysql_errno().") ".mysql_error(), $query$_SERVER['PHP_SELF'], __LINE__)); 
    This works exactly how I want it to, of course the errorfunction was taken off the php site to test with so needs more expanding but I have to say I'm glad that the CONSTANTS did the trick

    if you want a live preview then visit
    http://www.tikopets.org/error/test.php
    Last edited by Paramiliar; Jan 3rd, 2008 at 11:50 am. Reason: Automerged Doublepost
    Matthew Bagley
    Paramiliar Design Studios
    IT Consultant | Website Design | Website Development

  6. #6
    Idolized By All noppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nice noppid's Avatar
    Join Date
    October 12, 2006
    Location
    Florida
    Posts
    487
    Rep Power
    6
    Feedback Score
    0

    Default

    In your example, you invoke the error_controller function with die. You're not getting to the handler, just the trigger.

    The handler does not seem to be invoked on mysql as mysql calls no longer generate the warning state.

    I have more to say but have to run in the middle of typing. I'm interested in working this out for a wrapper myself. I'm tired of dealing with each query and want to make a handler.


    That's my thought anyway.
    Free vBulletin hacks and vBulletin Support.
    Talk to bikers around the world about motorcycles.
    Clogging an american dance step.

  7. #7
    Jedi Master Paramiliar is on a distinguished road Paramiliar's Avatar
    Join Date
    July 24, 2007
    Location
    West Midlands
    Posts
    105
    Rep Power
    5
    Feedback Score
    0

    Default

    noppid check the url I posted with the edit I just did to see the error message live on site

    because i use the die() command I can stop the script from carrying on which is what I want as its a fatal error, then I just output the error message and do what i need to with the error function

    the function sqlerrorhandler is acting as a bridge between the errorhandler and the error call

    Paramiliar added 120 Minutes and 28 Seconds later...

    also you can call the function without using die() so instead use

    PHP Code:
    or sqlerrorhandler("(".mysql_errno().") ".mysql_error(), $query$_SERVER['PHP_SELF'], __LINE__); 
    Last edited by Paramiliar; Jan 3rd, 2008 at 1:55 pm. Reason: Automerged Doublepost
    Matthew Bagley
    Paramiliar Design Studios
    IT Consultant | Website Design | Website Development

  8. #8
    Idolized By All noppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nicenoppid is just really nice noppid's Avatar
    Join Date
    October 12, 2006
    Location
    Florida
    Posts
    487
    Rep Power
    6
    Feedback Score
    0

    Default

    That's nice output. And everything you need called neatly. Very good.

    [me]Bookmarks Thread[/me]
    Free vBulletin hacks and vBulletin Support.
    Talk to bikers around the world about motorcycles.
    Clogging an american dance step.

  9. #9
    Jedi Master Paramiliar is on a distinguished road Paramiliar's Avatar
    Join Date
    July 24, 2007
    Location
    West Midlands
    Posts
    105
    Rep Power
    5
    Feedback Score
    0

    Default

    Quote Originally Posted by noppid View Post
    That's nice output. And everything you need called neatly. Very good.

    [me]Bookmarks Thread[/me]
    Thanks noppid, I've added it to the php site as well for people who want to do this in the future, its an ugly hack really but it get the job done! thanks everyone for your input!
    Matthew Bagley
    Paramiliar Design Studios
    IT Consultant | Website Design | Website Development

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
WebTalkForums
WebTalkForums
Recent Forum Threads