00001 <?php
00002
00003 class DbLib
00004 {
00005
00006 var $mHost = 'localhost';
00007 var $mPort = 3306;
00008 var $mUser = 'root';
00009 var $mPassword = '';
00010 var $mDatabase = 'db';
00011 var $mSocket;
00012 var $mResult;
00013 var $mRow;
00014
00015 function OpenConnection() { $this->mSocket = 1; }
00016 function SelectDatabase() { $this->IsConnected(); }
00017 function DbQuery() { $this->IsConnected(); }
00018 function Query() { $this->IsConnected(); }
00019 function GetResult() { $this->IsConnected(); }
00020 function GetNumRows() { $this->IsConnected(); }
00021 function GetNextRecord() { $this->IsConnected(); }
00022 function GetField() { $this->IsConnected(); }
00023 function GetFirstRelation() { $this->IsConnected(); }
00024 function GetLastId() { $this->IsConnected(); }
00025 function FreeResult() { $this->IsConnected(); }
00026 function CloseConnection() { $this->IsConnected(); }
00027 function GetError() { $this->IsConnected(); }
00028 function SqlCorrection() { $this->IsConnected(); }
00029 function __wakeup() { $this->OpenConnection(); }
00030 function __sleep() { $this->CloseConnection(); }
00031 function IsConnected() {
00032 if (!$this->mSocket) die('Not connected to database server');
00033 }
00034
00035 }
00036
00038
00039 class DbLibMySQL extends DbLib
00040 {
00041
00042 function init($host, $port, $user, $password, $database)
00043 {
00044 if ($host) $this->mHost = $host;
00045 if ($port) $this->mPort = $port;
00046 if ($user) $this->mUser = $user;
00047 if ($password) $this->mPassword = $password;
00048 if ($database) $this->mDatabase = $database;
00049 }
00050
00051 function OpenConnection()
00052 {
00053 parent::OpenConnection();
00054 $this->mSocket = @mysql_connect($this->mHost . ':' . $this->mPort, $this->mUser, $this->mPassword)
00055 or die($this->GetError());
00056 @mysql_select_db($this->mDatabase,$this->mSocket)
00057 or die($this->GetError());
00058 global $setnames;
00059 if (!$setnames)
00060 $this->Query('SET NAMES utf8');
00061 else
00062 $this->Query("SET NAMES '$setnames'");
00063 return $this->mSocket;
00064 }
00065
00066
00067 function SelectDatabase($database = '')
00068 {
00069 parent::SelectDatabase();
00070 @mysql_select_db($database,$this->mSocket) or die($this->GetError());
00071 $this->mDatabase = $database;
00072 }
00073
00074
00075 function DbQuery($query = '')
00076 {
00077 parent::DbQuery();
00078 @mysql_unbuffered_query($query,$this->mSocket) or die($this->GetError());
00079 }
00080
00081
00082 function Query($query = '')
00083 {
00084 parent::Query();
00085 ($this->mResult = mysql_query($query,$this->mSocket )) or die($this->GetError()) ;
00086 return $this->mResult;
00087 }
00088
00089
00090 function GetResult($row = 0, $coll = 0, $result = 0)
00091 {
00092 parent::GetResult();
00093 ($result) ? $temp_result = $result : $temp_result = $this->mResult;
00094
00095 if (!is_resource($temp_result))
00096 die('Not MySQL resource while getting results');
00097 return @mysql_result($temp_result,$row,$coll);
00098 }
00099
00100
00101 function GetNumRows($result = 0)
00102 {
00103 parent::GetNumRows();
00104 ($result) ? $temp_result = $result : $temp_result = $this->mResult;
00105
00106 if (!is_resource($temp_result))
00107 die('Not MySQL resource while getting results');
00108 return @mysql_num_rows($temp_result);
00109 }
00110 function GetLastId()
00111 {
00112 parent::GetLastId();
00113 return mysql_insert_id();
00114 }
00115
00116
00117 function GetNextRecord($result = 0)
00118 {
00119 parent::GetNextRecord();
00120 ($result) ? $temp_result = $result : $temp_result = $this->mResult;
00121
00122 if (!is_resource($temp_result))
00123 die('Not MySQL resource while getting results');
00124 $this->mRow = @mysql_fetch_assoc($temp_result);
00125 return $this->mRow != FALSE;
00126 }
00127
00128
00129 function GetField($field = '')
00130 {
00131 parent::GetField();
00132 return $this->mRow[$field];
00133 }
00134
00135
00136 function FreeResult($result = 0)
00137 {
00138 parent::FreeResult();
00139 if ($result) $temp_result = $result;
00140 else $temp_result = $this->mResult;
00141
00142 if (!is_resource($temp_result))
00143 die('Not MySQL resource while freeing results');
00144 @mysql_free_result($temp_result);
00145 }
00146
00147
00148 function SqlCorrection($string)
00149 {
00150 parent::SqlCorrection();
00151 return @mysql_real_escape_string($string);
00152 }
00153
00154
00155 function GetError()
00156 {
00157 $error = @mysql_error();
00158 $this->DbQuery('UNLOCK TABLES');
00159 parent::GetError();
00160 return $error;
00161 }
00162
00163
00164 function CloseConnection()
00165 {
00166 $this->DbQuery('UNLOCK TABLES');
00167 parent::CloseConnection();
00168 @mysql_close($this->mSocket) or die('There is no valid MySQL-Link resource');
00169 }
00170
00171 function GetFetchRow($result = 0)
00172 {
00173 parent::GetResult();
00174 ($result) ? $temp_result = $result : $temp_result = $this->mResult;
00175
00176 if (!is_resource($temp_result))
00177 die('Not MySQL resource while getting results');
00178 return @mysql_fetch_row($temp_result);
00179 }
00180
00181 function GetFetchAssoc($result = 0)
00182 {
00183 parent::GetResult();
00184 ($result) ? $temp_result = $result : $temp_result = $this->mResult;
00185
00186 if (!is_resource($temp_result))
00187 die('Not MySQL resource while getting results');
00188 return @mysql_fetch_assoc($temp_result);
00189 }
00190
00191 function GetFirstRelation($query)
00192 {
00193 parent::GetFirstRelation();
00194 $this->Query($query);
00195 $retarr = $this->GetFetchRow();
00196 if (!is_array($retarr)) {
00197 return null;
00198 }
00199 return $retarr[0];
00200 }
00201 }
00202 ?>