1 connection static $queries = 0; static $cachehits = 0; static $rowsf = 0; static $rowst = 0; static $time = 0; // Query debugging functions for admins static $connection_count = 0; static $debug_on = false; static $debug_list = array(); var $cache = array(); var $connection = NULL; var $id = 0; public function connect($host,$user,$pass,$persist=false) { $start=microtime(true); $this->connection = (($persist) ? mysql_pconnect($host,$user,$pass) : mysql_connect($host,$user,$pass)); $t = microtime(true)-$start; $this->id = ++self::$connection_count; $this->set_character_encoding("utf8"); if (self::$debug_on) { $b = self::getbacktrace(); self::$debug_list[] = array($this->id, $b['pfunc'], "$b[file]:$b[line]", "".(($persist)?"Persistent c":"C")."onnection established to mySQL server ($host, $user, using password: ".(($pass!=="") ? "YES" : "NO").")", sprintf("%01.6fs",$t)); } self::$time += $t; return $this->connection; } public function selectdb($dbname) { $start=microtime(true); $r = mysql_select_db($dbname, $this->connection); self::$time += microtime(true)-$start; return $r; } public function query($query, $usecache = false) { if ($usecache && array_key_exists($hash = md5($query), $this->cache)) { $start=microtime(true); ++self::$cachehits; @mysql_data_seek($this->cache[$hash], 0); $t = microtime(true)-$start; if (self::$debug_on) { $b = self::getbacktrace(); self::$debug_list[] = array($this->id, $b['pfunc'], "$b[file]:$b[line]", "$query", "".sprintf("%01.6fs",$t).""); } return $this->cache[$hash]; } $start=microtime(true); if($res = mysql_query($query, $this->connection)) { ++self::$queries; if (!is_bool($res)) self::$rowst += @mysql_num_rows($res); if ($usecache) { $this->cache[md5($query)] = &$res; } } else { // 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()); trigger_error("MySQL error: $err", E_USER_ERROR); } $t = microtime(true)-$start; self::$time += $t; if (self::$debug_on) { $b = self::getbacktrace(); $tx = ((!$err) ? $query : "$query"); self::$debug_list[] = array($this->id, $b['pfunc'], "$b[file]:$b[line]", $tx, sprintf("%01.6fs",$t)); } return $res; } public function fetch($result, $flag = MYSQL_BOTH){ $start=microtime(true); if($result && $res=mysql_fetch_array($result, $flag)) ++self::$rowsf; self::$time += microtime(true)-$start; return $res; } public function result($result,$row=0,$col=0){ $start=microtime(true); if($result) { if (mysql_num_rows($result) < $row+1) $res = NULL; elseif ($res=@mysql_result($result,$row,$col)) ++self::$rowsf; } self::$time += microtime(true)-$start; return $res; } public function fetchq($query, $flag = MYSQL_BOTH, $cache = false){ $res = $this->query($query, $cache); $res = $this->fetch($res, $flag); return $res; } public function resultq($query,$row=0,$col=0, $cache = false){ $res = $this->query($query, $cache); $res = $this->result($res,$row,$col); return $res; } public function getmultiresults($query, $key, $wanted, $cache = false) { $q = $this->query($query, $cache); $ret = array(); $tmp = array(); while ($res = @$this->fetch($q, MYSQL_ASSOC)) $tmp[$res[$key]][] = $res[$wanted]; foreach ($tmp as $keys => $values) $ret[$keys] = implode(",", $values); return $ret; } public function getresultsbykey($query, $key, $wanted, $cache = false) { $q = $this->query($query, $cache); $ret = array(); while ($res = @$this->fetch($q, MYSQL_ASSOC)) $ret[$res[$key]] = $res[$wanted]; return $ret; } public function getresults($query, $wanted, $cache = false) { $q = $this->query($query, $cache); $ret = array(); while ($res = @$this->fetch($q, MYSQL_ASSOC)) $ret[] = $res[$wanted]; return $ret; } public function getarraybykey($query, $key, $cache = false) { $q = $this->query($query, $cache); $ret = array(); while ($res = @$this->fetch($q, MYSQL_ASSOC)) $ret[$res[$key]] = $res; return $ret; } public function getarray($query, $cache = false) { $q = $this->query($query, $cache); $ret = array(); while ($res = @$this->fetch($q, MYSQL_ASSOC)) $ret[] = $res; return $ret; } public function escape($s) { return mysql_real_escape_string($s); } public function set_character_encoding($s) { return mysql_set_charset($s, $this->connection); } //private function __construct() {} // Debugging shit for admins public static function debugprinter() { global $tccellh, $tccellc, $tccell1, $tccell2, $tblstart, $smallfont, $tblend; if (!self::$debug_on) return ""; $out = ""; $out .= "
$tblstart$tccellh colspan=5>SQL Debug $tccellh width=20>  $tccellh width=20>ID $tccellh width=300>Function $tccellh width=*>Query $tccellh width=90>Time"; foreach(self::$debug_list as $i => $d) { $altcell = "tccell" . (($i & 1)+1); $cell = $$altcell; if ($oldid && $oldid != $d[0]) $out .= "$tccellc colspan=5>"; $oldid = $d[0]; $out .= " $cell>$i $cell>$d[0] $cell>$d[1]$smallfont
$d[2] $cell style='white-space: pre-wrap; text-align: left'>$d[3] $cell>$d[4]"; } $out .= "$tblend"; return $out; } private static function getbacktrace() { $backtrace = debug_backtrace(); for ($i = 1; isset($backtrace[$i]); ++$i) { if (substr($backtrace[$i]['file'], -9) !== "mysql.php") { if (!($backtrace[$i]['pfunc'] = $backtrace[$i+1]['function'])) $backtrace[$i]['pfunc'] = "(main)"; $backtrace[$i]['file'] = str_replace($_SERVER['DOCUMENT_ROOT'], "", $backtrace[$i]['file']); return $backtrace[$i]; } } return $backtrace[$i-1]; } } ?>