you can now specify your own error handling function by changing DB::$error_handler
This commit is contained in:
29
db.class.php
29
db.class.php
@@ -17,6 +17,7 @@ class DB
|
|||||||
public static $host = 'localhost';
|
public static $host = 'localhost';
|
||||||
public static $encoding = 'latin1';
|
public static $encoding = 'latin1';
|
||||||
public static $queryMode = 'queryAllRows'; //buffered, unbuffered, queryAllRows
|
public static $queryMode = 'queryAllRows'; //buffered, unbuffered, queryAllRows
|
||||||
|
public static $error_handler = 'meekrodb_error_handler';
|
||||||
|
|
||||||
public static function get($dbName = '') {
|
public static function get($dbName = '') {
|
||||||
static $mysql = null;
|
static $mysql = null;
|
||||||
@@ -290,14 +291,16 @@ class DB
|
|||||||
$result = $db->query($sql, $is_buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT);
|
$result = $db->query($sql, $is_buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT);
|
||||||
if (DB::$debug) $runtime = microtime(true) - $starttime;
|
if (DB::$debug) $runtime = microtime(true) - $starttime;
|
||||||
|
|
||||||
$sqlShow = "$sql (" . ($is_buffered ? 'MYSQLI_STORE_RESULT' : 'MYSQLI_USE_RESULT') . ")";
|
|
||||||
if (!$sql || $error = DB::checkError()) {
|
if (!$sql || $error = DB::checkError()) {
|
||||||
echo "ATTEMPTED QUERY: $sqlShow<br>\n";
|
if (function_exists(DB::$error_handler)) {
|
||||||
echo "ERROR: $error<br>\n";
|
call_user_func(DB::$error_handler, array(
|
||||||
debug_print_backtrace();
|
'query' => $sql,
|
||||||
die;
|
'error' => $error
|
||||||
|
));
|
||||||
|
}
|
||||||
} else if (DB::$debug) {
|
} else if (DB::$debug) {
|
||||||
$runtime = sprintf('%f', $runtime * 1000);
|
$runtime = sprintf('%f', $runtime * 1000);
|
||||||
|
$sqlShow = "$sql (" . ($is_buffered ? 'MYSQLI_STORE_RESULT' : 'MYSQLI_USE_RESULT') . ")";
|
||||||
echo "QUERY: $sqlShow [$runtime ms]<br>\n";
|
echo "QUERY: $sqlShow [$runtime ms]<br>\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -482,4 +485,20 @@ class DBTransaction {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function meekrodb_error_handler($params) {
|
||||||
|
$out[] = "QUERY: " . $params['query'];
|
||||||
|
$out[] = "ERROR: " . $params['error'];
|
||||||
|
$out[] = "";
|
||||||
|
|
||||||
|
if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
|
||||||
|
echo implode("\n", $out);
|
||||||
|
} else {
|
||||||
|
echo implode("<br>\n", $out);
|
||||||
|
}
|
||||||
|
|
||||||
|
debug_print_backtrace();
|
||||||
|
|
||||||
|
die;
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -1,4 +1,11 @@
|
|||||||
<?
|
<?
|
||||||
|
function new_error_callback($params) {
|
||||||
|
global $error_callback_worked;
|
||||||
|
|
||||||
|
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $error_callback_worked = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class BasicTest extends SimpleTest {
|
class BasicTest extends SimpleTest {
|
||||||
function __construct() {
|
function __construct() {
|
||||||
error_reporting(E_ALL);
|
error_reporting(E_ALL);
|
||||||
@@ -95,6 +102,15 @@ class BasicTest extends SimpleTest {
|
|||||||
$results = DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
|
$results = DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
|
||||||
$this->assert(count($results) === 2);
|
$this->assert(count($results) === 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_5_error_handler() {
|
||||||
|
global $error_callback_worked;
|
||||||
|
|
||||||
|
DB::$error_handler = 'new_error_callback';
|
||||||
|
DB::query("SELET * FROM accounts");
|
||||||
|
|
||||||
|
$this->assert($error_callback_worked === 1);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user