MySQL Error Function Wrapper for PHP

by damonp on February 7, 2011

in Development,PHP,SQL

I was recently asked to supply some development and debugging functionality to a web application built for a client. The following function can be configured to popup for only your IP, and gives an error message containing the SQL, line number and file for easy debugging of the error.

I like this function over my current hobbled together debug kit because the display is positioned top center of the page via CSS no matter where the error occurs. Plus it includes a hide button to hide the majority of the error output and remove button to remove the error display altogether.

<?php
    function mysql_error_wrapper($q, $sql='', $line='', $file='')   {
        if($_SERVER['REMOTE_ADDR'] != 'YOUR LOCAL IP')  return false;
?>
<script type="text/javascript" language="JavaScript"><!--
function showhide(d) {
    if(document.getElementById(d).style.display == "none") {
        document.getElementById(d).style.display = "block";
        document.getElementById('shde').innerHTML = "hide";
    }   else {
        document.getElementById(d).style.display = "none";
        document.getElementById('shde').innerHTML = "show";    
    }
}

function remove()   {
    document.getElementById('errorblock').style.display = "none";
}
//--></script>

<?php
        echo '<div id="errorblock" style="display:block;position:absolute;top:10px;left:10%;width:80%;background:#efefef;text-align:left;border:1px solid #CD0000;padding:10px;">';
        echo '<a href="#" onclick="showhide(\'sqlerror\')"><span id="shde">hide</span></a> - <a href="#" onclick="remove();">remove</a>';
        echo '<h3 style="color:#CD0000;">MySQL error: '.mysql_errno().' <small style="font-weight:normal;">Line: #'.$line.' '.basename($file).'</small></h3>';
       
        echo '<pre id="sqlerror" style="height:250px;overflow:auto;padding:10px;">';
       
        echo wordwrap(mysql_error());
       
        if($sql != '')  {
            echo '<h4>SQL</h4>'.wordwrap($sql);
        }

        if($line != '') {
            echo '<h4>Line</h4>'.$line;
        }
       
        if($file != '') {
            echo '<h4>File</h4>'.$file;
        }
                   
        echo '</pre>';
        echo '</div>';         
   
    }
?>

Usage example:

<?php
    $sql = "YOUR SQL STATEMENT";
    $q = mysql_query($sql, $db);
    if(!$q) mysql_error_wrapper($q, $sql, __LINE__, __FILE__);
?>

Popularity: 3%

More Related Posts

Damon Parker is a freelance sysadmin and web developer in Texas. He specializes in server setup, server security and high performance server configurations. Need help setting up a web server or getting a server back online after a crash or hack? Email Damon

{ 1 comment… read it below or add one }

Stefan Gabos April 19, 2011 at 12:17 pm

You don’t need to add __LINE__ and __FILE__ to you function – use PHP’s debug_backtrace function to get those automatically.

Also, you might want to chech out this MySQL wrapper at http://stefangabos.ro/php-libraries/zebra-database/. It has a very nice debugging console (among lots of other useful things). It may be helpful for further developing your own function

Reply

Leave a Comment

Previous post: