1<?php 2 3/* 4 [UCenter] (C)2001-2099 Comsenz Inc. 5 This is NOT a freeware, use is subject to license terms 6 7 $Id: db.class.php 1171 2014-11-03 03:33:47Z hypowang $ 8*/ 9 10 11class ucclient_db { 12 var $querynum = 0; 13 var $link; 14 var $histories; 15 16 var $dbhost; 17 var $dbuser; 18 var $dbpw; 19 var $dbcharset; 20 var $pconnect; 21 var $tablepre; 22 var $time; 23 24 var $goneaway = 5; 25 26 function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset = '', $pconnect = 0, $tablepre='', $time = 0) { 27 $this->dbhost = $dbhost; 28 $this->dbuser = $dbuser; 29 $this->dbpw = $dbpw; 30 $this->dbname = $dbname; 31 $this->dbcharset = $dbcharset; 32 $this->pconnect = $pconnect; 33 $this->tablepre = $tablepre; 34 $this->time = $time; 35 36 if($pconnect) { 37 if(!$this->link = mysql_pconnect($dbhost, $dbuser, $dbpw)) { 38 $this->halt('Can not connect to MySQL server'); 39 } 40 } else { 41 if(!$this->link = mysql_connect($dbhost, $dbuser, $dbpw)) { 42 $this->halt('Can not connect to MySQL server'); 43 } 44 } 45 46 if($this->version() > '4.1') { 47 if($dbcharset) { 48 mysql_query("SET character_set_connection=".$dbcharset.", character_set_results=".$dbcharset.", character_set_client=binary", $this->link); 49 } 50 51 if($this->version() > '5.0.1') { 52 mysql_query("SET sql_mode=''", $this->link); 53 } 54 } 55 56 if($dbname) { 57 mysql_select_db($dbname, $this->link); 58 } 59 60 } 61 62 function fetch_array($query, $result_type = MYSQL_ASSOC) { 63 return mysql_fetch_array($query, $result_type); 64 } 65 66 function result_first($sql) { 67 $query = $this->query($sql); 68 return $this->result($query, 0); 69 } 70 71 function fetch_first($sql) { 72 $query = $this->query($sql); 73 return $this->fetch_array($query); 74 } 75 76 function fetch_all($sql, $id = '') { 77 $arr = array(); 78 $query = $this->query($sql); 79 while($data = $this->fetch_array($query)) { 80 $id ? $arr[$data[$id]] = $data : $arr[] = $data; 81 } 82 return $arr; 83 } 84 85 function cache_gc() { 86 $this->query("DELETE FROM {$this->tablepre}sqlcaches WHERE expiry<$this->time"); 87 } 88 89 function query($sql, $type = '', $cachetime = FALSE) { 90 $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query'; 91 if(!($query = $func($sql, $this->link)) && $type != 'SILENT') { 92 $this->halt('MySQL Query Error', $sql); 93 } 94 $this->querynum++; 95 $this->histories[] = $sql; 96 return $query; 97 } 98 99 function affected_rows() { 100 return mysql_affected_rows($this->link); 101 } 102 103 function error() { 104 return (($this->link) ? mysql_error($this->link) : mysql_error()); 105 } 106 107 function errno() { 108 return intval(($this->link) ? mysql_errno($this->link) : mysql_errno()); 109 } 110 111 function result($query, $row) { 112 $query = @mysql_result($query, $row); 113 return $query; 114 } 115 116 function num_rows($query) { 117 $query = mysql_num_rows($query); 118 return $query; 119 } 120 121 function num_fields($query) { 122 return mysql_num_fields($query); 123 } 124 125 function free_result($query) { 126 return mysql_free_result($query); 127 } 128 129 function insert_id() { 130 return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0); 131 } 132 133 function fetch_row($query) { 134 $query = mysql_fetch_row($query); 135 return $query; 136 } 137 138 function fetch_fields($query) { 139 return mysql_fetch_field($query); 140 } 141 142 function version() { 143 return mysql_get_server_info($this->link); 144 } 145 146 function escape_string($str) { 147 return mysql_escape_string($str); 148 } 149 150 function close() { 151 return mysql_close($this->link); 152 } 153 154 function halt($message = '', $sql = '') { 155 $error = mysql_error(); 156 $errorno = mysql_errno(); 157 if($errorno == 2006 && $this->goneaway-- > 0) { 158 $this->connect($this->dbhost, $this->dbuser, $this->dbpw, $this->dbname, $this->dbcharset, $this->pconnect, $this->tablepre, $this->time); 159 $this->query($sql); 160 } else { 161 $s = ''; 162 if($message) { 163 $s = "<b>UCenter info:</b> $message<br />"; 164 } 165 if($sql) { 166 $s .= '<b>SQL:</b>'.htmlspecialchars($sql).'<br />'; 167 } 168 $s .= '<b>Error:</b>'.$error.'<br />'; 169 $s .= '<b>Errno:</b>'.$errorno.'<br />'; 170 $s = str_replace(UC_DBTABLEPRE, '[Table]', $s); 171 exit($s); 172 } 173 } 174} 175 176?>