qResult = $res; $this->PageSize = 0; $this->id = ''; $this->iterator = 0; $this->CurrentPage = 0; $this->RowsCount = mysql_num_rows($this->qResult); } public function GetRow(){ if($this->PageSize!=0 && !empty($this->id)){ $this->iterator ++; if($this->iterator > $this->PageSize){ return false; }else return mysql_fetch_assoc($this->qResult); }else return mysql_fetch_assoc($this->qResult); } public function Prepare($id, $size){ global $main; $this->id = $id; $this->PageSize = $size; $currGet = isset($main->app->request[$this->id]) ? (int)$main->app->request[$this->id] : -1; $currCook = isset($_COOKIE[$this->id]) ? (int)$_COOKIE[$this->id] : 0; if($currGet != -1){ $this->CurrentPage = $currGet; setcookie($this->id, $currGet, null, '/'); }else{ $this->CurrentPage = $currCook; } $row = $this->CurrentPage * $this->PageSize; if($this->RowsCount > $row){ @mysql_data_seek($this->qResult, $row); } } public function BuildPager(){ global $main; $pagesCount = (int)ceil($this->RowsCount / $this->PageSize); $result = array(); $parturl = "/go/" . $main->app->path . "/"; foreach ($main->app->request as $key => $value){ if($this->id == $key) continue; $parturl .= $key . '_' . $value . '/'; } for($i = 1; $i <= $pagesCount; $i++){ $url = $parturl . $this->id . "_" . ($i - 1) . "/index.html"; array_push($result, array("n" => $i, "url" => $url, "curr" => $this->CurrentPage == ($i - 1) ? "y" : "n")); } return $result; } } class cMySQL{ private $clink; public $id; private $isUpdate; private $isSelect; private $isInsert; private $isDelete; private $isParametred; public $Err; private $isConnected; private $sql; private $qResult; public $rowsCount; public $version; function __construct(){ $this->version = '1.1.1'; $this->refresh(); } function refresh(){ $this->id = ''; $this->isUpdate = false; $this->isSelect = false; $this->isInsert = false; $this->isDelete = false; $this->isParametred = false; if($this->isConnected) mysql_close($this->clink); $this->isConnected = false; $this->Err = array(); $this->sql = ''; if(isset($this->qResult))unset($this->qResult); $this->rowsCount = 0; if(isset($this->clink))unset($this->clink); } function Connect($server, $user, $password, $db){ $this->refresh(); if(!($this->clink = @mysql_connect($server, $user, $password, true))){ $this->Error('Error connect to server: ' . $server); return false; } if(!mysql_select_db($db, $this->clink)){ if(mysql_errno($this->clink) == 1049) if(mysql_query("CREATE DATABASE $db", $this->clink) === false){ $this->Error(mysql_errno($this->clink) . ': ' . mysql_error($this->clink)); return false; }elseif(!mysql_select_db($db, $this->clink)){ $this->Error("DB",mysql_errno($this->clink) . ': ' . mysql_error($this->clink)); return false; }else{ $this->isConnected = true; return true; } }else{ $this->isConnected = true; return true; } } function Prepare($SQL){ if(!$this->isConnected)return false; $this->sql = $SQL; $this->isParametred = true; $this->isSelect = false; $this->isInsert = false; $this->isUpdate = false; $this->isDelete = false; $this->rowsCount = 0; $this->id = 0; if(preg_match('/\@\w/', $this->sql))$this->isParametred = true; if(preg_match('/^[\n\s\r\t]*select/i', $this->sql))$this->isSelect = true; if(preg_match('/^[\n\s\r\t]*insert/i', $this->sql))$this->isInsert = true; if(preg_match('/^[\n\s\r\t]*update/i', $this->sql))$this->isUpdate = true; if(preg_match('/^[\n\s\r\t]*delete/i', $this->sql))$this->isDelete = true; } function SetParamValue($param, $val){ if(!$this->isConnected)return false; if(!$this->isParametred)return false; $qouted = mysql_real_escape_string($val); if(!mysql_query("SET @$param=\"$qouted\"", $this->clink)){ $this->Error(mysql_errno($this->clink) . ': ' . mysql_error($this->clink)); return false; } return true; } function SetParamsFromHash($hash){ if(!is_array($hash))return false; foreach($hash as $key => $val) if(!$this->SetParamValue($key, $val))return false; return true; } function Execute(){ if(!$this->isConnected){ $this->Error("not connected"); return false; } $this->rowsCount = 0; if(($this->qResult = mysql_query($this->sql, $this->clink)) === false){ $this->Error(mysql_errno($this->clink) . ': ' . mysql_error($this->clink)); return false; }else{ if($this->isInsert){ $this->id = mysql_insert_id($this->clink); } if($this->isSelect) $this->rowsCount = mysql_num_rows($this->qResult); else $this->rowsCount = mysql_affected_rows($this->clink); return true; } } function GetRow($GetHash = true){ if(!is_resource($this->qResult))return false; if($GetHash){ return mysql_fetch_assoc($this->qResult); }else{ return mysql_fetch_array($this->qResult, MYSQL_NUM); } } function GetResource(){ if(!$this->isSelect || !is_resource($this->qResult))return null; if(isset($GLOBALS['main'])){ $res = &new cDBResource($this->qResult); }else{ return null; } return $res; } function Val($findex = 0){ if($this->rowsCount != 1) return false; $tmp = mysql_fetch_array($this->qResult); return $tmp[$findex]; } function TableExists($tableName){ if(!$this->isConnected){ $this->Error("not connected"); return false; } if(($qResult = mysql_query("SHOW TABLES", $this->clink)) === false){ $this->Error(mysql_errno($this->clink) . ': ' . mysql_error($this->clink)); return false; }else{ while($row = mysql_fetch_row($qResult)){ if($row[0] == $tableName) return true; } return false; } } function isActive(){ return $this->isConnected; } function Error($msg){ if(isset($GLOBALS['main'])){ global $main; $main->app->RaiseError("DB", $msg); }else{ $this->Err[] = $msg; } } //aliases for some function function p($SQL){ return $this->Prepare($SQL); } function s($param, $val){ return $this->SetParamValue($param, $val); } function sh($hash){ return $this->SetParamsFromHash($hash); } function e(){ return $this->Execute(); } function g($GetHash = true){ return $this->GetRow($GetHash); } function a(){ return $this->isActive(); } } ?>