changing lib/mysql.php line endings + adding db char encoding func

This commit is contained in:
Xkeeper
2015-08-09 04:19:45 -07:00
parent da7c205682
commit 9e43949067

View File

@@ -1,210 +1,215 @@
<?php <?php
class mysql { class mysql {
// a 'backport' of my 'static' class in not-as-static form // a 'backport' of my 'static' class in not-as-static form
// the statistics remain static so they're global just in case this gets used for >1 connection // the statistics remain static so they're global just in case this gets used for >1 connection
static $queries = 0; static $queries = 0;
static $cachehits = 0; static $cachehits = 0;
static $rowsf = 0; static $rowsf = 0;
static $rowst = 0; static $rowst = 0;
static $time = 0; static $time = 0;
// Query debugging functions for admins // Query debugging functions for admins
static $connection_count = 0; static $connection_count = 0;
static $debug_on = false; static $debug_on = false;
static $debug_list = array(); static $debug_list = array();
var $cache = array(); var $cache = array();
var $connection = NULL; var $connection = NULL;
var $id = 0; var $id = 0;
public function connect($host,$user,$pass,$persist=false) { public function connect($host,$user,$pass,$persist=false) {
$start=microtime(true); $start=microtime(true);
$this->connection = (($persist) ? mysql_pconnect($host,$user,$pass) : mysql_connect($host,$user,$pass)); $this->connection = (($persist) ? mysql_pconnect($host,$user,$pass) : mysql_connect($host,$user,$pass));
$t = microtime(true)-$start; $t = microtime(true)-$start;
$this->id = ++self::$connection_count; $this->id = ++self::$connection_count;
if (self::$debug_on) { if (self::$debug_on) {
$b = self::getbacktrace(); $b = self::getbacktrace();
self::$debug_list[] = array($this->id, $b['pfunc'], "$b[file]:$b[line]", "<i>".(($persist)?"Persistent c":"C")."onnection established to mySQL server ($host, $user, using password: ".(($pass!=="") ? "YES" : "NO").")</i>", sprintf("%01.6fs",$t)); self::$debug_list[] = array($this->id, $b['pfunc'], "$b[file]:$b[line]", "<i>".(($persist)?"Persistent c":"C")."onnection established to mySQL server ($host, $user, using password: ".(($pass!=="") ? "YES" : "NO").")</i>", sprintf("%01.6fs",$t));
} }
self::$time += $t; self::$time += $t;
return $this->connection; return $this->connection;
} }
public function selectdb($dbname) { public function selectdb($dbname) {
$start=microtime(true); $start=microtime(true);
$r = mysql_select_db($dbname, $this->connection); $r = mysql_select_db($dbname, $this->connection);
self::$time += microtime(true)-$start; self::$time += microtime(true)-$start;
return $r; return $r;
} }
public function query($query, $usecache = false) { public function query($query, $usecache = false) {
if ($usecache && array_key_exists($hash = md5($query), $this->cache)) { if ($usecache && array_key_exists($hash = md5($query), $this->cache)) {
$start=microtime(true); $start=microtime(true);
++self::$cachehits; ++self::$cachehits;
@mysql_data_seek($this->cache[$hash], 0); @mysql_data_seek($this->cache[$hash], 0);
$t = microtime(true)-$start; $t = microtime(true)-$start;
if (self::$debug_on) { if (self::$debug_on) {
$b = self::getbacktrace(); $b = self::getbacktrace();
self::$debug_list[] = array($this->id, $b['pfunc'], "$b[file]:$b[line]", "<font color=#00dd00>$query</font>", "<font color=#00dd00>".sprintf("%01.6fs",$t)."</font>"); self::$debug_list[] = array($this->id, $b['pfunc'], "$b[file]:$b[line]", "<font color=#00dd00>$query</font>", "<font color=#00dd00>".sprintf("%01.6fs",$t)."</font>");
} }
return $this->cache[$hash]; return $this->cache[$hash];
} }
$start=microtime(true); $start=microtime(true);
if($res = mysql_query($query, $this->connection)) { if($res = mysql_query($query, $this->connection)) {
++self::$queries; ++self::$queries;
if (!is_bool($res)) if (!is_bool($res))
self::$rowst += @mysql_num_rows($res); self::$rowst += @mysql_num_rows($res);
if ($usecache) { if ($usecache) {
$this->cache[md5($query)] = &$res; $this->cache[md5($query)] = &$res;
} }
} }
else { else {
// the huge SQL warning text sucks // the huge SQL warning text sucks
$err = str_replace("You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use", "SQL syntax error", mysql_error()); $err = str_replace("You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use", "SQL syntax error", mysql_error());
trigger_error("MySQL error: $err", E_USER_ERROR); trigger_error("MySQL error: $err", E_USER_ERROR);
} }
$t = microtime(true)-$start; $t = microtime(true)-$start;
self::$time += $t; self::$time += $t;
if (self::$debug_on) { if (self::$debug_on) {
$b = self::getbacktrace(); $b = self::getbacktrace();
$tx = ((!$err) ? $query : "<span style=\"color:#FF0000;border-bottom:1px dotted red;\" title=\"$err\">$query</span>"); $tx = ((!$err) ? $query : "<span style=\"color:#FF0000;border-bottom:1px dotted red;\" title=\"$err\">$query</span>");
self::$debug_list[] = array($this->id, $b['pfunc'], "$b[file]:$b[line]", $tx, sprintf("%01.6fs",$t)); self::$debug_list[] = array($this->id, $b['pfunc'], "$b[file]:$b[line]", $tx, sprintf("%01.6fs",$t));
} }
return $res; return $res;
} }
public function fetch($result, $flag = MYSQL_BOTH){ public function fetch($result, $flag = MYSQL_BOTH){
$start=microtime(true); $start=microtime(true);
if($result && $res=mysql_fetch_array($result, $flag)) if($result && $res=mysql_fetch_array($result, $flag))
++self::$rowsf; ++self::$rowsf;
self::$time += microtime(true)-$start; self::$time += microtime(true)-$start;
return $res; return $res;
} }
public function result($result,$row=0,$col=0){ public function result($result,$row=0,$col=0){
$start=microtime(true); $start=microtime(true);
if($result) { if($result) {
if (mysql_num_rows($result) < $row+1) if (mysql_num_rows($result) < $row+1)
$res = NULL; $res = NULL;
elseif ($res=@mysql_result($result,$row,$col)) elseif ($res=@mysql_result($result,$row,$col))
++self::$rowsf; ++self::$rowsf;
} }
self::$time += microtime(true)-$start; self::$time += microtime(true)-$start;
return $res; return $res;
} }
public function fetchq($query, $flag = MYSQL_BOTH, $cache = false){ public function fetchq($query, $flag = MYSQL_BOTH, $cache = false){
$res = $this->query($query, $cache); $res = $this->query($query, $cache);
$res = $this->fetch($res, $flag); $res = $this->fetch($res, $flag);
return $res; return $res;
} }
public function resultq($query,$row=0,$col=0, $cache = false){ public function resultq($query,$row=0,$col=0, $cache = false){
$res = $this->query($query, $cache); $res = $this->query($query, $cache);
$res = $this->result($res,$row,$col); $res = $this->result($res,$row,$col);
return $res; return $res;
} }
public function getmultiresults($query, $key, $wanted, $cache = false) { public function getmultiresults($query, $key, $wanted, $cache = false) {
$q = $this->query($query, $cache); $q = $this->query($query, $cache);
$ret = array(); $ret = array();
$tmp = array(); $tmp = array();
while ($res = @$this->fetch($q, MYSQL_ASSOC)) while ($res = @$this->fetch($q, MYSQL_ASSOC))
$tmp[$res[$key]][] = $res[$wanted]; $tmp[$res[$key]][] = $res[$wanted];
foreach ($tmp as $keys => $values) foreach ($tmp as $keys => $values)
$ret[$keys] = implode(",", $values); $ret[$keys] = implode(",", $values);
return $ret; return $ret;
} }
public function getresultsbykey($query, $key, $wanted, $cache = false) { public function getresultsbykey($query, $key, $wanted, $cache = false) {
$q = $this->query($query, $cache); $q = $this->query($query, $cache);
$ret = array(); $ret = array();
while ($res = @$this->fetch($q, MYSQL_ASSOC)) while ($res = @$this->fetch($q, MYSQL_ASSOC))
$ret[$res[$key]] = $res[$wanted]; $ret[$res[$key]] = $res[$wanted];
return $ret; return $ret;
} }
public function getresults($query, $wanted, $cache = false) { public function getresults($query, $wanted, $cache = false) {
$q = $this->query($query, $cache); $q = $this->query($query, $cache);
$ret = array(); $ret = array();
while ($res = @$this->fetch($q, MYSQL_ASSOC)) while ($res = @$this->fetch($q, MYSQL_ASSOC))
$ret[] = $res[$wanted]; $ret[] = $res[$wanted];
return $ret; return $ret;
} }
public function getarraybykey($query, $key, $cache = false) { public function getarraybykey($query, $key, $cache = false) {
$q = $this->query($query, $cache); $q = $this->query($query, $cache);
$ret = array(); $ret = array();
while ($res = @$this->fetch($q, MYSQL_ASSOC)) while ($res = @$this->fetch($q, MYSQL_ASSOC))
$ret[$res[$key]] = $res; $ret[$res[$key]] = $res;
return $ret; return $ret;
} }
public function getarray($query, $cache = false) { public function getarray($query, $cache = false) {
$q = $this->query($query, $cache); $q = $this->query($query, $cache);
$ret = array(); $ret = array();
while ($res = @$this->fetch($q, MYSQL_ASSOC)) while ($res = @$this->fetch($q, MYSQL_ASSOC))
$ret[] = $res; $ret[] = $res;
return $ret; return $ret;
} }
public function escape($s) { public function escape($s) {
return mysql_real_escape_string($s); return mysql_real_escape_string($s);
} }
//private function __construct() {}
public function set_character_encoding($s) {
// Debugging shit for admins return mysql_set_charset($s, $this->connection);
public static function debugprinter() { }
global $tccellh, $tccellc, $tccell1, $tccell2, $tblstart, $smallfont, $tblend;
if (!self::$debug_on) return ""; //private function __construct() {}
$out = "";
$out .= "<br>$tblstart<tr>$tccellh colspan=5><b>SQL Debug</b></td><tr> // Debugging shit for admins
$tccellh width=20>&nbsp</td> public static function debugprinter() {
$tccellh width=20>ID</td> global $tccellh, $tccellc, $tccell1, $tccell2, $tblstart, $smallfont, $tblend;
$tccellh width=300>Function</td> if (!self::$debug_on) return "";
$tccellh width=*>Query</td> $out = "";
$tccellh width=90>Time</td></tr>"; $out .= "<br>$tblstart<tr>$tccellh colspan=5><b>SQL Debug</b></td><tr>
foreach(self::$debug_list as $i => $d) { $tccellh width=20>&nbsp</td>
$altcell = "tccell" . (($i & 1)+1); $tccellh width=20>ID</td>
$cell = $$altcell; $tccellh width=300>Function</td>
if ($oldid && $oldid != $d[0]) $tccellh width=*>Query</td>
$out .= "<tr>$tccellc colspan=5><img src='images/_.gif' height='4' width='1'></td></tr>"; $tccellh width=90>Time</td></tr>";
$oldid = $d[0]; foreach(self::$debug_list as $i => $d) {
$out .= "<tr> $altcell = "tccell" . (($i & 1)+1);
$cell>$i</td> $cell = $$altcell;
$cell>$d[0]</td> if ($oldid && $oldid != $d[0])
$cell>$d[1]$smallfont<br>$d[2]</font></td> $out .= "<tr>$tccellc colspan=5><img src='images/_.gif' height='4' width='1'></td></tr>";
$cell style='white-space: pre-wrap; text-align: left'>$d[3]</td> $oldid = $d[0];
$cell>$d[4]</td></tr>"; $out .= "<tr>
} $cell>$i</td>
$out .= "$tblend"; $cell>$d[0]</td>
return $out; $cell>$d[1]$smallfont<br>$d[2]</font></td>
} $cell style='white-space: pre-wrap; text-align: left'>$d[3]</td>
$cell>$d[4]</td></tr>";
private static function getbacktrace() { }
$backtrace = debug_backtrace(); $out .= "$tblend";
for ($i = 1; isset($backtrace[$i]); ++$i) { return $out;
if (substr($backtrace[$i]['file'], -9) !== "mysql.php") { }
if (!($backtrace[$i]['pfunc'] = $backtrace[$i+1]['function']))
$backtrace[$i]['pfunc'] = "<i>(main)</i>"; private static function getbacktrace() {
$backtrace[$i]['file'] = str_replace($_SERVER['DOCUMENT_ROOT'], "", $backtrace[$i]['file']); $backtrace = debug_backtrace();
return $backtrace[$i]; for ($i = 1; isset($backtrace[$i]); ++$i) {
} if (substr($backtrace[$i]['file'], -9) !== "mysql.php") {
} if (!($backtrace[$i]['pfunc'] = $backtrace[$i+1]['function']))
return $backtrace[$i-1]; $backtrace[$i]['pfunc'] = "<i>(main)</i>";
} $backtrace[$i]['file'] = str_replace($_SERVER['DOCUMENT_ROOT'], "", $backtrace[$i]['file']);
} return $backtrace[$i];
?> }
}
return $backtrace[$i-1];
}
}
?>