- 001
- 002
- 003
- 004
- 005
- 006
- 007
- 008
- 009
- 010
- 011
- 012
- 013
- 014
- 015
- 016
- 017
- 018
- 019
- 020
- 021
- 022
- 023
- 024
- 025
- 026
- 027
- 028
- 029
- 030
- 031
- 032
- 033
- 034
- 035
- 036
- 037
- 038
- 039
- 040
- 041
- 042
- 043
- 044
- 045
- 046
- 047
- 048
- 049
- 050
- 051
- 052
- 053
- 054
- 055
- 056
- 057
- 058
- 059
- 060
- 061
- 062
- 063
- 064
- 065
- 066
- 067
- 068
- 069
- 070
- 071
- 072
- 073
- 074
- 075
- 076
- 077
- 078
- 079
- 080
- 081
- 082
- 083
- 084
- 085
- 086
- 087
- 088
- 089
- 090
- 091
- 092
- 093
- 094
- 095
- 096
- 097
- 098
- 099
- 100
<?php defined('START') or die;
# For Licence and Disclaimer of this code, see http://coresky.net/license
# Filename: unique
# Portname: mysql_ php module (clear cloud)
function trace($var, $show_error = false, $line = 0, $file = '', $context = null) {
global $sky;
if (!isset($sky)) return;
if ($sky->debug || $sky->s_prod_error && true === $show_error) {
is_string($var) or $var = var_export($var, true);
if (is_string($show_error)) $var = "$show_error: $var" and $show_error = false;
if ($has_deep = !$file) {
$deep = 1 + $line;
$trace = debug_backtrace();
$file = $trace[$line]['file'];
$line = $trace[$line]['line'];
}
$error = "$file:$line:\n" . html($var);
if ($show_error) {
$sky->was_arror = true;
if (CLI) echo $has_deep ? "\n$file:$line:\n$var\n\n" : "\n$var\n\n";
if ($sky->s_prod_error) {
$sky->error_prod .= NOW . ' - ' . START;
if (in_array(START, ['front', 'admin'])) $sky->error_prod .= ' uri: ' . html(URI);
$sky->error_prod .= "\n$error\n\n";
}
}
if ($sky->debug) {
if ($show_error) {
$sky->tracing .= "$file:$line:\n" . '<span class="error">' . html($var) . "</span>\n";
ob_start();
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$backtrace = html(ob_get_clean());
if ($sky->error_on_page) {
$sky->errors .= sprintf("<h1>%s</h1><pre>$error\n\n$backtrace</pre>", $sky->error_title ? $sky->error_title : 'User Error');
$sky->error_title = '';
if ($str = $sky->context($context, $has_deep ? $deep : 2)) $sky->errors .= "<pre>$str</pre>";
} else {
$sky->tracing .= "BACKTRACE:\n$backtrace\n";
if (AJAX) exit;
if (!CLI) echo "<br /><b>SKY:</b> " . html($var) . " at <b>$file</b> on line <b>$line</b>";
}
$sky->tracing .= "\n";
} else $sky->tracing .= "$error\n\n";
}
}
}
function sql($sql, $deep = 1) {
global $sky;
if (in_array($mode = $sql[0], ['+', '-', '~', '@', '%', '^', '>'])) $sql = substr($sql, 1);
if ($run = '^' != $mode) $result = mysql_query($sql);
if (($sky->debug || $sky->s_prod_error) && $deep && $run) {
$sky->qn++;
if ($no = mysql_errno()) $sky->error_title = 'SQL Error';
trace("SQL: $sql" . ($no ? ",\nERROR in MySQL - $no: " . mysql_error() : ''), (bool)$no, $deep);
}
switch (strtolower(substr($sql, 0, 6))) {
case 'delete': case 'update': case 'replac': return mysql_affected_rows();
case 'insert': return mysql_insert_id();
case 'select': switch ($mode) {
case '+': return mysql_result($result, 0);
case '-': return mysql_fetch_row($result);
case '~': return mysql_fetch_assoc($result);
case '@': return fetch($result, 0);
case '%': return fetch($result, 0, 'assoc');
case '^': return sprintf("\$q = sql('%s', %d);", escape($sql, "'\\"), 1 + $deep) . fetch;
case '>': return ($r = mysql_fetch_assoc($result)) ? (object)$r : null;
}
}
return $result;
}
function sqlf() {
$ary = func_get_args();
$func = 'return is_array($v) ? _join($v, ", ", "' . strtolower(substr($sql = array_shift($ary), 0, 1)) . '") : $v;';
return sql(vsprintf($sql, array_map(create_function('$v', $func), $ary)), 2);
}
function fetch($q, $p = false, $func = 'row') {
if (is_bool($p)) return $p ? mysql_fetch_row($q) : mysql_fetch_assoc($q);
$func = "mysql_fetch_$func";
for ($cnt = mysql_num_fields($q), $ary = []; $r = $func($q); ) {
$key = current(array_splice($r, $p, 1));
if (1 == $cnt) $ary[] = $key;
else $ary[$key] = 2 == $cnt ? current($r) : $r;
}
return $ary; # mysql_free_result($q);
}
define('fetch', 'if ($r = mysql_fetch_assoc($q)) extract($r, EXTR_PREFIX_ALL, "r"); return $r;');
function cnt($q, $rows = true) {
return $rows ? mysql_num_rows($q) : mysql_num_fields($q);
}
function _join($ary, $by = ', ', $x = false) {
$keys = array_keys($ary);
$vals = array_values($ary);
Комментарии (0) RSS
Добавить комментарий