<?php
define('ERR_DB_NOT_CONNECTED', 'Íåâîçìîæíî ñîåäèíèòñÿ ñ ñåðâåðîì ÁÄ');
class MySQL {
private static $_instance;
protected $_hdb = false;
protected $_dbname;
protected $_dbinfo;
protected $_query;
protected $_querySnapshot;
protected $_mode = MYSQL_BOTH;
protected $_error = 'OK';
protected $_errorNo = '000';
protected $_errorStatus = false;
protected $_ver = "1.2";
private function __construct($data = array()){
$this->_hdb = @mysql_connect($data['server'], $data['username'], $data['password']) or die(ERR_DB_NOT_CONNECTED);
$this->SetFetchMode(MYSQL_BOTH);
}
public function CreateInstance($server = 'localhost', $username='root', $password='', $single_mode = true){
$data['server']=$server;
$data['username']=$username;
$data['password']=$password;
if ($single_mode && self::$_instance == null || !self::$_instance instanceof MySQL) {
self::$_instance = new MySQL($data);
} else {
self::$_instance = new MySQL($data);
}
return self::$_instance;
}
function __destruct(){
if (is_resource($this->_hdb)) {
@mysql_close($this->_hdb);
}
}
private function SetError(){
if (is_resource($this->_hdb)) {
$this->_errorNo = @mysql_errno($this->_hdb);
$this->_error = @mysql_error($this->_hdb);
if ($this->_errorNo !== 0) $this->Fatal();
}
}
private function _SetDBInfo(){
$this->_dbinfo = @mysql_stat($this->_hdb);
}
public function GetDBInfo(){
$this->SetDBInfo();
return $this->_dbinfo;
}
public function SetDB($dbname) {
if (!is_resource($this->_hdb)) {
return false;
}
return @mysql_select_db($dbname, $this->_hdb);
}
public function SetFetchMode($mode = MYSQL_BOTH){
$this->_mode = $mode;
}
public function SetEncoding($charset = 'cp1251'){
return $this->Query("set names $charset");
}
function Query($query = ''){
$this->_querySnapshot = $query;
if (is_resource($this->_hdb)) {
$result = @mysql_query($query, $this->_hdb) or $this->setError();
$this->_query = (is_resource($result)) ? $result : false;
}
return $this->_query;
}
public function Select($query = ''){
return $this->Query($query);
}
public function FetchRow($handle = null){
if (!$handle) {
$result = @mysql_fetch_row($handle);
} else {
$result = @mysql_fetch_row($this->_query);
}
return $result;
}
public function FetchArray($handle = null){
return ($handle != null) ? @mysql_fetch_array($handle, $this->_mode) : @mysql_fetch_array($this->_query, $this->_mode);
}
public function FetchResult($handle = null, $row = 0, $field = null){
return ($handle != null) ? @mysql_result($handle, $row, $field) : @mysql_result($this->_query, $row, $field);
}
public function FetchObject($handle = null){
return ($handle != null) ? @mysql_fetch_object($handle) : @mysql_fetch_object($this->_query);
}
public function FetchAll($limit = 0, $handle = null){
$iterator = 0;
while ($row = $this->FetchArray($handle)){
$iterator += 1;
$result[] = $row;
if ($limit > 0 && $limit == $iterator) break;
}
return $result;
}
public function GetRowCount($handle = null){
return ($handle != null) ? @mysql_num_rows($handle) : @mysql_num_rows($this->_query);
}
public function GetRecordsCount($handle = null){
return ($handle != null) ? @mysql_num_rows($handle) : @mysql_num_rows($this->_query);
}
public function GetRowCountQuery($query){
$this->Query($query);
if (is_resource($this->_query)) {
return $this->GetRowCount();
} else return false;
}
public function GetRowCountTable($table, $condition = "1=1"){
$this->Query("select 1 from $table where $condition");
if (is_resource($this->_query)) {
return $this->GetRowCount();
} else return false;
}
public function FreeResult($handle = null){
return ($handle != null) ? @mysql_free_result($handle) : @mysql_free_result($this->_query);
}
public function GetNumFields($handle = null){
return ($handle != null) ? @mysql_num_fields($handle) : @mysql_num_fields($this->_query);
}
private function _insertID(){
if (is_resource($this->_hdb)) {
return @mysql_insert_id($this->_hdb);
} else return false;
}
private function _affectedRows(){
if (is_resource($this->_hdb)) {
return @mysql_affected_rows($this->_hdb);
} else return false;
}
public function getInsertedID(){return $this->_insertID();}
public function getAffectedRows(){return $this->_affectedRows();}
public function getErrorInfo(){return 'MYSQL-' . $this->_errorNo . " " . $this->_error;}
public function InsertRow($table, $fields = array(), $values = array(), $returning = true){
$_f = (count($fields)==0) ? '':'('.join(",", $fields).')';
$_v = join(",", $values);
$result = $this->Query("insert into $table $_f values($_v)");
if ($returning){
return $this->getInsertedID();
} else {
return $result;
}
}
public function InsertScope($table, $scope, $returning = true){
$result = $this->Query("insert into $table $scope");
if ($returning) {return $this->getAffectedRows();} else return true;
}
public function Update($table, $data = array(), $condition = '1=1', $returning = true){
$set = join(",", $data);
$result = $this->Query("update $table set $set where $condition");
if ($returning) {
return $this->getAffectedRows();
} else {
return $result;
}
}
public function Delete($table, $condition='1=1', $returning = true){
$result = $this->Query("delete from $table where $condition");
if ($returning) {
return $this->getAffectedRows();
} else {
return $result;
}
}
public function Truncate($table){
if (!is_resource($this->_hdb)) {
return false;
}
return $this->Query("truncate table $table");
}
public function Drop($object, $name){
if (!is_resource($this->_hdb)) {
return false;
}
return $this->Query("drop $object $name");
}
public function StartTransaction(){
if (!is_resource($this->_hdb)) {
return false;
}
$this->Query("SET AUTOCOMMIT=0");
$this->Query("START TRANSACTION");
return true;
}
public function Commit(){
if (!is_resource($this->_hdb)) {
return false;
}
$this->Query("COMMIT");
return true;
}
public function Rollback(){
if (!is_resource($this->_hdb)) {
return false;
}
$this->Query("ROLLBACK");
return true;
}
function Fatal(){
echo "<h4><font color='red'>ÎØÈÁÊÀ!!!</font></h4>";
echo "<p>
Âûïîëíåíèå çàïðîñà: <font color='blue'>{$this->_querySnapshot}</font> áûëî îñòàíîâëåíî èç-çà îøèáêè.<br /><br />
".$this->getErrorInfo()."
</p>";
}
function GetQuerySnapshot(){
return $this->_querySnapshot;
}
function GetVersion(){
return $this->_ver;
}
}
?>