00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00034 Class Mysql {
00035
00036 var $link_id = 0;
00037 var $query_id = 0;
00038 var $aRecord = array();
00039
00040 var $strError = '';
00041 var $intErrno = 0;
00042 var $booShowError = true;
00043 var $strLogfile = 'mysql_error.log';
00044 var $intQueryCount = 0;
00045
00046 var $tblActionlog;
00047 var $tblGuild;
00048 var $tblMap;
00049 var $tblMapData;
00050 var $tblPlayer;
00051 var $tblTown;
00052 var $tblUser;
00053 var $tblUsergroup;
00054
00063 function Mysql ( $strServer, $strUser, $strPass, $strDB ) {
00064 if ( 0 == $this->link_id ) {
00065 $this->link_id = mysql_connect ( $strServer, $strUser, $strPass );
00066
00067 if ( !$this->link_id ) {
00068 $this->print_error ( 'Link-ID == false, connect failed' );
00069 return false;
00070 }
00071 if ( $strDB != '' ) {
00072 return $this->select_db ( $strDB );
00073 }
00074 return true;
00075 }
00076 return false;
00077 }
00078
00083 function setPrefix( $str )
00084 {
00085 $this->tblActionlog = $str.'actionlog';
00086 $this->tblGuild = $str.'guild';
00087 $this->tblMap = $str.'map';
00088 $this->tblMapData = $str.'map_data';
00089 $this->tblPlayer = $str.'player';
00090 $this->tblTown = $str.'town';
00091 $this->tblUser = $str.'user';
00092 $this->tblUsergroup = $str.'usergroup';
00093 }
00094
00099 function getError() {
00100 $this->strError = mysql_error();
00101 return $this->strError;
00102 }
00103
00108 function getErrno() {
00109 $this->intErrno = mysql_errno();
00110 return $this->intErrno;
00111 }
00112
00118 function select_db ( $strDB = '' ) {
00119 if ( !@mysql_select_db ( $strDB, $this->link_id ) ) {
00120 $this->print_error ( 'cannot use database '.$strDB );
00121 return false;
00122 }
00123 return true;
00124 }
00125
00131 function query ( $strQuery ) {
00132 $this->query_id = @mysql_query ( $strQuery, $this->link_id );
00133
00134 $this->intQueryCount++;
00135 if ( !$this->query_id ) {
00136 $this->print_error( $strQuery );
00137 return false;
00138 }
00139 return $this->query_id;
00140 }
00141
00146 function fetch_array ( $query_id = -1 ) {
00147 if ( $query_id != -1 ) {
00148 $this->query_id = $query_id;
00149 }
00150 $this->record = @mysql_fetch_array ( $this->query_id );
00151 return $this->record;
00152 }
00153
00158 function fetch_assoc ( $query_id = -1 ) {
00159 if ( $query_id != -1 ) {
00160 $this->query_id = $query_id;
00161 }
00162 $this->record = @mysql_fetch_assoc ( $this->query_id );
00163 return $this->record;
00164 }
00165
00170 function fetch_row ( $query_id = -1 ) {
00171 if ( $query_id != -1 ) {
00172 $this->query_id = $query_id;
00173 }
00174 $this->record = @mysql_fetch_row ( $this->query_id );
00175 return $this->record;
00176 }
00177
00181 function free_result ( $query_id = -1 ) {
00182 if ( $query_id != -1 ) {
00183 $this->query_id = $query_id;
00184 }
00185 return @mysql_free_result ( $this->query_id );
00186 }
00187
00193 function query_first ( $strQuery ) {
00194 $this->query ( $strQuery );
00195 $returnarray = $this->fetch_array($this->query_id);
00196 $this->free_result($this->query_id);
00197 return $returnarray;
00198 }
00199
00204 function num_rows ( $query_id = -1 ) {
00205 if ( $query_id != -1 ) {
00206 $this->query_id = $query_id;
00207 }
00208 return @mysql_num_rows ( $this->query_id );
00209 }
00210
00215 function affected_rows() {
00216 return @mysql_affected_rows();
00217 }
00218
00223 function insert_id() {
00224 return @mysql_insert_id ( $this->link_id );
00225 }
00226
00231 function print_error ( $msg )
00232 {
00233 global $strTmpl_inhalt, $strTmplNav;
00234 $this->getError();
00235 $this->getErrno();
00236
00237 if ( $this->intErrno == 1146 && isset ( $GLOBALS['oStyle'] ) )
00238 {
00239
00240 $oStyle =& $GLOBALS['oStyle'];
00241 $oStyle->addReplacements ( array ( 'DB_TABLE' => $this->strError ) );
00242 $strTmpl_inhalt = $oStyle->get_area ( 'NoDbMsg' );
00243 $strTmplNav = '';
00244 output();
00245 }
00246
00247 $line = 0;
00248 $file = '';
00249 $debug = debug_backtrace();
00250 for ( $i = 0; $i < count ( $debug ); $i++ ) {
00251 if ( $debug[$i]['file'] != __FILE__ ) {
00252 $line = $debug[$i]['line'];
00253 $file = $debug[$i]['file'];
00254 $i = count ( $debug );
00255 }
00256 }
00257
00258 if( $this->strLogfile != '' && is_writable( $this->strLogfile ) )
00259 {
00260 $logfile = fopen ( $this->strLogfile, 'a' );
00261
00262 $msg = str_replace("\n", '', str_replace("\r\n", '', $msg) );
00263 $error = sprintf ('%s : #%04d - %s in Datei: %s, Zeile: %d %s', date("Y/m/d H:i:s"), $this->intErrno, '"'.$msg.'" Meldung: '.$this->strError, $file, $line, "\n");
00264 fputs ( $logfile, $error );
00265 fclose( $logfile);
00266 }
00267
00268 $message = '<pre>';
00269 $message .= "<b>Database error!</b>\n";
00270 $message .= "SQL Query: $msg\n";
00271 $message .= "mysql error: $this->strError\n";
00272 $message .= "mysql error number: $this->intErrno\n";
00273 $message .= "Date: ".date("d.m.Y @ H:i")."\n";
00274 if ( $line > 0 && $file ) {
00275 $message .= "File: <b>".$file.'</b> on line <b>'.$line."</b>\n";
00276 }
00277 $message .= "Referer: ".getenv('HTTP_REFERER')."\n";
00278 if( $this->strLogfile != '' && !is_writable( $this->strLogfile ) )
00279 {
00280 $message .= "Fehlerlog konnte nicht geschrieben werden.";
00281 }
00282 $message .= "</pre>\n";
00283
00284 if ( $this->booShowError ) {
00285 @ob_end_clean();
00286 @ob_start();
00287 die($message);
00288 } else {
00289 echo "\n<!-- SQL Fehler: $msg -->\n";
00290 }
00291 }
00292 }