Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //// Initialize the sql library...
- //
- $sql = new sql;
- $sql->appname = 'www.furaffinity.net';
- $sql->database = 'furaffinity';
- $sql->server = '192.168.1.1';
- $sql->user = 'furaffinity';
- $sql->password = 'kayWrolv~quoles6';
- $sql->logfile = '/srv/www/logs/mysql-error.log';
- $sql->connect();
- return $sql;
- class sql
- {
- var $database = '';
- var $linkid;
- var $query_result;
- var $record;
- var $logfile;
- var $errdesc;
- var $errno;
- var $show_error;
- var $server;
- var $user;
- var $password;
- //// Store all executed queries internally for debugging purposes
- //
- var $query_count;
- var $query_history;
- var $_transaction_started;
- function sql()
- {
- $this->database = '';
- $this->logfile = '';
- $this->linkid = 0;
- $this->query_result = 0;
- $this->record = array();
- $this->errdesc = '';
- $this->errno = 0;
- $this->show_error = 1;
- $this->server = '';
- $this->user = '';
- $this->password = '';
- $this->query_count = 0;
- $this->query_history = array();
- $this->_transaction_started = FALSE;
- }
- function connect()
- {
- if($this->linkid == 0)
- {
- $this->linkid = @mysql_connect($this->server, $this->user, $this->password);
- $this->linkid
- or
- $this->print_error("Link-ID == false, connect failed");
- ($this->database and $this->linkid)
- and
- $this->select_db($this->database);
- return $this->linkid;
- }
- }
- function disconnect()
- {
- mysql_close($this->linkid);
- }
- function sql_error()
- {
- $this->error = mysql_error();
- return $this->error;
- }
- function geterrno()
- {
- $this->errno=mysql_errno();
- return $this->errno;
- }
- function fetch_row($query_result=-1)
- {
- $query_result != -1
- and
- $this->query_result = $query_result;
- $this->record = mysql_fetch_row($this->query_result);
- return $this->record;
- }
- function select_db($database = '')
- {
- $database
- and
- $this->database = $database;
- @mysql_select_db($this->database, $this->linkid)
- or
- $this->print_error('cannot use database '.$this->database);
- }
- function query($query_string, $query_tag = '', $query_file = '', $ignore_errors = FALSE)
- {
- //// Free the previuos query result, if it happends to still exist
- //
- $this->query_result
- and
- @mysql_free_result($this->query_result);
- //// Add a comment to the query string, so the query could be
- // easily traceable via mysql "show processlist"
- //
- $comment = '';
- $query_file
- and
- $comment .= '['.basename($query_file).'] ';
- $query_tag
- and
- $comment .= $query_tag;
- $comment
- and
- $comment = '/* '.$comment.' */ ';
- $query_string = $comment.$query_string;
- $query_start = microtime();
- $this->query_result = mysql_query($query_string, $this->linkid);
- $query_end = microtime();
- $used_time = $this->_microtime_used($query_start, $query_end);
- if($ignore_errors == FALSE)
- {
- $this->query_result
- or
- $this->print_error("Invalid SQL: ".$query_string);
- }
- $this->query_history[$this->query_count]['sql'] = $query_string;
- $this->query_history[$this->query_count]['tag'] = $query_tag;
- $this->query_history[$this->query_count]['file'] = $query_file;
- $this->query_history[$this->query_count]['time'] = $used_time;
- $this->query_count++;
- return $this->query_result;
- }
- function fetch_array($query_result=-1)
- {
- $query_result != -1
- and
- $this->query_result=$query_result;
- $this->record = mysql_fetch_array($this->query_result, MYSQL_ASSOC);
- return $this->record;
- }
- function free_result($query_result=-1)
- {
- $query_result != -1
- and
- $this->query_result = $query_result;
- return @mysql_free_result($this->query_result);
- }
- function insertid($field, $table, $condition, $condval)
- {
- for($i=0; $i!=$val; $i++)
- {
- $val = $sql->query_first("SELECT MAX($field)as maxid FROM $table WHERE $condition='$condval'");
- if($val['maxid'])
- {
- return $val['maxid'];
- }
- }
- }
- function count($columnname, $from, $condition='')
- {
- $condition = ($condition) ? ' WHERE '.$condition : '';
- $var = $this->query_first('SELECT count('.$columnname.') AS thiscount FROM '.$from.' '.$condition);
- $var = $var['thiscount'];
- return $var;
- }
- function get_maxid($columnname, $table, $condition)
- {
- $condition = ($condition) ? ' WHERE '.$condition : '';
- $var = $this->query_first('SELECT max('.$columnname.') AS thismax FROM '.$table.' '.$condition);
- $var = $var['thismax']+1;
- return $var;
- }
- function num_rows($query_result=-1)
- {
- $query_result!=-1
- and
- $this->query_result=$query_result;
- return mysql_num_rows($this->query_result);
- }
- function affected_rows()
- {
- return mysql_affected_rows($this->linkid);
- }
- function insert_id()
- {
- return mysql_insert_id($this->linkid);
- }
- function count_queries()
- {
- return $this->query_count;
- }
- function print_error($msg)
- {
- global $adminmail, $basepath, $_USER, $header, $footer, $_BASE;
- $this->errdesc = mysql_error();
- $this->errno = mysql_errno();
- $this->errdesc == 'Got error 127 from table handler'
- and
- DoTableScan();
- $this->errno == 1203
- and
- $error->sysSendError('013');
- if($this->errno == 1040)
- {
- echo "<font size='2'><b>System Error</b></font><br/>Database responded: Too many connections!";
- exit();
- }
- $message = "Database error in $this->appname: $msg\n";
- $message .= "mysql error: $this->errdesc\n";
- $message .= "mysql error number: $this->errno\n";
- $message .= "User: ".$_USER['username']."\n";
- $message .= "Date: ".date("d.m.Y @ H:i")."\n";
- $message .= "Script: ".getenv("REQUEST_URI")."\n";
- $message .= "Referer: ".getenv("HTTP_REFERER")."\n";
- $this->_log($message);
- if($_USER['accesslevel'] == "1")
- {
- $adminerror = "<br/><br/>
- <div align='center'>
- <table cellpadding='3' cellspacing='1' border='0' class='maintable'>
- <tr>
- <td class='cat'>Access to query information is granted!</td>
- </tr>
- <tr>
- <td class='alt1'>".nl2br($message)."</td>
- </tr>
- </table>
- </div>";
- }
- eval("dooutput(\"".tpl("db_error","htm",1)."\");");
- die("");
- }
- function _microtime_used(&$before, &$after)
- {
- return (substr($after, 11) - substr($before, 11)) + (substr($after, 0, 9) - substr($before, 0, 9));
- }
- function generate_report($admin=0)
- {
- $cnt = count($this->query_history);
- $total_time = 0;
- $report = '<pre>'.
- BR.' SQL runtime report'.
- BR.' <hr>'.
- BR.' '.str_pad('Tag', 65, ' ').str_pad('Run time', 10, ' ').'Query'.
- BR.' <hr>';
- for ($i=0; $i<$cnt; $i++)
- {
- $query = $this->query_history[$i];
- $tag = '';
- $query['file']
- and
- $tag .= '['.basename($query['file']).'] ';
- $query['tag']
- and
- $tag .= $query['tag'];
- $report .= BR.
- ' '.
- str_pad($tag , 65, ' ').
- str_pad(round($query['time'], 5), 10, ' ').
- substr($query['sql'], strpos($query['sql'], '*/')+3);
- $total_time += $query['time'];
- }
- $report .= BR.
- ' <hr>'.
- ' Total time taken for SQL: '.(round($total_time, 5)).' sec'.
- ' <hr>'.
- '</pre>';
- return $report;
- }
- function run_time()
- {
- $total_time = 0;
- $cnt = count($this->query_history);
- for($i=0; $i<$cnt; $i++)
- $total_time += $this->query_history[$i]['time'];
- return $total_time;
- }
- function transaction_start()
- {
- if($this->_transaction_started == TRUE)
- $this->transaction_end();
- $query = 'SET AUTOCOMMIT=0';
- $this->query($query, 'start_transaction', __FILE__);
- $query = 'BEGIN';
- $this->query($query, 'start_transaction', __FILE__);
- $this->_transaction_started = TRUE;
- }
- function transaction_end()
- {
- $query = 'COMMIT';
- $this->query($query, 'end_transaction', __FILE__);
- $this->_transaction_started = FALSE;
- }
- //
- //
- //
- // Functions to use in the future.
- // Obsolete the functions declared above.
- //
- //
- //
- //
- // Internal
- //
- function _log($message)
- {
- global $ip;
- file_put_contents($this->logfile, $message.' IP ADDRESS: '.$ip."\n\n\n", FILE_APPEND);
- }
- //
- // String manipulation
- //
- function real_escape_string($text)
- {
- return mysql_real_escape_string($text, $this->linkid);
- }
- function qstr($text)
- {
- return "'".$this->real_escape_string($text)."'";
- }
- //
- // Query shortcuts
- //
- function get_all($query, $desc=FALSE, $file=FALSE)
- {
- $result = array();
- $rst = $this->query($query, $desc, $file);
- if($rst)
- {
- while($row = $this->fetch_array($rst))
- $result[] = $row;
- $this->free_result($rst);
- }
- return $result;
- }
- function get_row($query, $desc=FALSE, $file=FALSE)
- {
- $result = $this->query($query, $desc, $file);
- if($result == FALSE)
- return FALSE;
- return $this->fetch_array($result);
- }
- function query_first($query_string, $query_tag = '', $query_file = '')
- {
- $this->query($query_string, $query_tag, $query_file);
- $returnarray=$this->fetch_array($this->query_result);
- $this->free_result($this->query_result);
- return $returnarray;
- }
- function get_one($query_string, $query_tag = '', $query_file = '')
- {
- $this->query($query_string, $query_tag, $query_file);
- $tmp = $this->fetch_row($this->query_result);
- $this->free_result($this->query_result);
- return $tmp[0];
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement