PHP Conference Kansai 2025

Voting

: min(seven, one)?
(Example: nine)

The Note You're Voting On

webmaster at paramiliar dot com
17 years ago
We needed to use an error handler to handle SQL errors while passing the query along so the query is also logged and this is what we came up with, its kind of an ugly bridge but it works 100%

<?php

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
sqlerrorhandler("(".mysql_errno().") ".mysql_error(), $query, $_SERVER['PHP_SELF'], __LINE__);


?>

<< Back to user notes page

To Top