Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

mysql.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  This program is free software; you can redistribute it and/or
00004  modify it under the terms of the GNU General Public License
00005  as published by the Free Software Foundation; either version 2
00006  of the License, or (at your option) any later version.
00007 
00008  This program is distributed in the hope that it will be useful,
00009  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  GNU General Public License for more details.
00012 
00013  You should have received a copy of the GNU General Public License
00014  along with this program; if not, write to the Free Software
00015  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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             // table does not exist, print special error
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             // strip line breaks
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 }

Generated on Sun May 8 19:29:45 2005 for PhpMap by  doxygen 1.4.2