rem_lex

mysql.class.php

Sep 3rd, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.36 KB | None | 0 0
  1. <?php
  2. $userROW['status'] = 1;
  3. class mysql {
  4.  
  5.         function connect($host, $user, $pass, $db, $noerror = 0, $charset) {
  6.                 global $lang, $timer;
  7.  
  8.                 $this->queries = 0;
  9.                 $this->query_list = array();
  10.                 $this->error = 0;
  11.                 $this->conn_db = $db;
  12.                 $this->queryTimer = (isset($timer) && (method_exists($timer, 'stop')));
  13.  
  14.                 $this->connect = @mysql_connect($host, $user, $pass, true) or die('<h1>An Error Occurred</h1><hr />Unable to connect to the database!');
  15. //              @mysql_query("/*!40101 SET NAMES 'cp1251' */", $this->connect);
  16.                 @mysql_query("/*!40101 SET NAMES " . $charset . " */", $this->connect);
  17.                 if (!@mysql_select_db($db)) {
  18.                         if (!$noerror) {
  19.                                 die('<h1>An Error Occurred</h1><hr />Unable to find the database <i>'.$db.'</i>!');
  20.                         }
  21.                         $this->error = 1;
  22.                 }
  23.         }
  24.  
  25.         // Report an SQL error
  26.         // $type        - query type
  27.         // $query       - text of the query
  28.         function errorReport($type, $query){
  29.                 global $userROW, $lang, $config;
  30.  
  31.                 if (!$noerror) {
  32.                         print "<div style='font: 12px verdana; background-color: #EEEEEE; border: #ABCDEF 1px solid; margin: 1px; padding: 3px;'><span style='color: red;'>MySQL ERROR [".$type."]: ".$query."</span><br/><span style=\"font: 9px arial;\">(".mysql_errno($this->connect).'): '.mysql_error($this->connect).'</span></div>';
  33.                 } else {
  34.                         print "<div style='font: 12px verdana; background-color: #EEEEEE; border: #ABCDEF 1px solid; margin: 1px; padding: 3px;'><span style='color: red;'>MySQL ERROR [".$type."]: *** (you don't have a permission to see this error) ***</span></span></div>";
  35.                 }
  36.         }
  37.  
  38.         function select($sql, $assocMode = 0) {
  39.                 global $timer;
  40.                 if ($this->queryTimer) { $tX = $timer->stop(4); }
  41.  
  42.                 $this->queries++;
  43.                 if (!($query = @mysql_query($sql, $this->connect))) {
  44.                         $this->errorReport('select', $sql);
  45.                         return array();
  46.                 }
  47.  
  48.                 $result = array();
  49.  
  50.                 switch ($assocMode) {
  51.                         case -1: $am = MYSQL_NUM; break;
  52.                         case  1: $am = MYSQL_ASSOC; break;
  53.                         case  0:
  54.                         default: $am = MYSQL_BOTH;
  55.                 }
  56.  
  57.                 while($item = mysql_fetch_array($query, $am)) {
  58.                         $result[] = $item;
  59.                 }
  60.  
  61.                 if ($this->queryTimer) { $tX = '[ '.($timer->stop(4) - $tX).' ] '; } else { $tX = ''; }
  62.                 array_push ($this->query_list, $tX.$sql);
  63.  
  64.                 return $result;
  65.         }
  66.  
  67.         function record($sql, $assocMode = 0) {
  68.                 global $timer;
  69.                 if ($this->queryTimer) { $tX = $timer->stop(4); }
  70.  
  71.                 $this->queries++;
  72.                 if (!($query = @mysql_query($sql, $this->connect))) {
  73.                         $this->errorReport('record', $sql);
  74.                         return array();
  75.                 }
  76.                 switch ($assocMode) {
  77.                         case -1: $am = MYSQL_NUM; break;
  78.                         case  1: $am = MYSQL_ASSOC; break;
  79.                         case  0:
  80.                         default: $am = MYSQL_BOTH;
  81.                 }
  82.  
  83.                 $item = mysql_fetch_array($query, $am);
  84.  
  85.                 if ($this->queryTimer) { $tX = '[ '.($timer->stop(4) - $tX).' ] '; } else { $tX = ''; }
  86.                 array_push ($this->query_list, $tX.$sql);
  87.  
  88.                 return $item;
  89.         }
  90.  
  91.         function query($sql) {
  92.                 global $timer;
  93.  
  94.             if ($this->queryTimer) { $tX = $timer->stop(4); }
  95.                 $this->queries++;
  96.                 if (!($query = @mysql_query($sql, $this->connect))) {
  97.                         $this->errorReport('query', $sql);
  98.                         return array();
  99.                 }
  100.  
  101.                 if ($this->queryTimer) { $tX = '[ '.($timer->stop(4) - $tX).' ] '; } else { $tX = ''; }
  102.                 array_push ($this->query_list, $tX.$sql);
  103.  
  104.                 return $query;
  105.         }
  106.  
  107.         function result($sql) {
  108.                 global $timer;
  109.                 if ($this->queryTimer) { $tX = $timer->stop(4); }
  110.  
  111.                 $this->queries++;
  112.                 if (!($query = @mysql_query($sql, $this->connect))) {
  113.                         $this->errorReport('result', $sql);
  114.                         return false;
  115.                 }
  116.  
  117.  
  118.                 if ($this->queryTimer) { $tX = '[ '.($timer->stop(4) - $tX).' ] '; } else { $tX = ''; }
  119.                 array_push ($this->query_list, $tX.$sql);
  120.  
  121.                 if ($query) {
  122.                         return @mysql_result($query, 0);
  123.                 }
  124.         }
  125.  
  126.         // check if table exists
  127.         function table_exists($table, $forceReload = 0) {
  128.                 // Check if data are already saved
  129.                 if (is_array($this->table_list)  && !$forceReload) {
  130.                         return $this->table_list[$table]?1:0;
  131.                 }
  132.  
  133.                 $this->table_list=array();
  134.  
  135.                 if (!($query = @mysql_query("show tables", $this->connect))) {
  136.                         $this->errorReport('select', $sql);
  137.                         return false;
  138.                 }
  139.  
  140.                 while($item = mysql_fetch_array($query, MYSQL_NUM)) {
  141.                         $this->table_list[$item[0]] = 1;
  142.                 }
  143.  
  144.                 return $this->table_list[$table]?1:0;
  145.         }
  146.  
  147.         function affected_rows() {
  148.                 return mysql_affected_rows($this->connect);
  149.         }
  150.  
  151.         function qcnt() {
  152.                 return $this->queries;
  153.         }
  154.  
  155.         function lastid($table = '') {
  156.                 if ($table != '') {
  157.                         $row = $this->record("SHOW TABLE STATUS LIKE '".prefix."_".$table."'");
  158.                         return ($row['Auto_increment'] - 1);
  159.                 } else {
  160.                         return mysql_insert_id($this->connect);
  161.                 }
  162.         }
  163.  
  164.         function close() {
  165.                 @mysql_close($this->connect);
  166.         }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment