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.
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:
$sql = "YOUR SQL STATEMENT";
$q = mysql_query($sql, $db);
if(!$q) mysql_error_wrapper($q, $sql, __LINE__, __FILE__);
?>
{ 1 comment }