- 001
 - 002
 - 003
 - 004
 - 005
 - 006
 - 007
 - 008
 - 009
 - 010
 - 011
 - 012
 - 013
 - 014
 - 015
 - 016
 - 017
 - 018
 - 019
 - 020
 - 021
 - 022
 - 023
 - 024
 - 025
 - 026
 - 027
 - 028
 - 029
 - 030
 - 031
 - 032
 - 033
 - 034
 - 035
 - 036
 - 037
 - 038
 - 039
 - 040
 - 041
 - 042
 - 043
 - 044
 - 045
 - 046
 - 047
 - 048
 - 049
 - 050
 - 051
 - 052
 - 053
 - 054
 - 055
 - 056
 - 057
 - 058
 - 059
 - 060
 - 061
 - 062
 - 063
 - 064
 - 065
 - 066
 - 067
 - 068
 - 069
 - 070
 - 071
 - 072
 - 073
 - 074
 - 075
 - 076
 - 077
 - 078
 - 079
 - 080
 - 081
 - 082
 - 083
 - 084
 - 085
 - 086
 - 087
 - 088
 - 089
 - 090
 - 091
 - 092
 - 093
 - 094
 - 095
 - 096
 - 097
 - 098
 - 099
 - 100
 
                        //Функция обработки ошибок PHP
set_error_handler('error_php'); 
function error_php($errno, $errstr, $errfile, $errline) 
{ 
    if (!error_reporting()) 
    { 
        return; 
    } 
     switch ($errno) 
    { 
        case E_WARNING: 
        case E_USER_WARNING: 
            $errfile = str_replace(getcwd(), '', $errfile); 
            require(ROOT_DIR.'/messages/errors/error_php.php'); 
            exit;
        break; 
    }         
} 
class mysql_db {
	
    private $db;
  
  function __construct() { //Метод вызываемый автоматически для присоединения к MySQL и выбора БД.
    $this->db=mysql_pconnect(MySQL_Host, MySQL_User, MySQL_Pass) or mysql_err('Не удалось соединиться с MySql.','Проверьте настройки параметров - MySql_Host, MySql_User, MySql_Pass в файле config.php');
    @mysql_select_db(MySQL_DB,$this->db)  or $this->mysql_err('Не удалось выбрать БД.','#'.mysql_errno().': '.mysql_error());
    @mysql_query('SET NAMES '.$this->str_sql(MySQL_Character), $this->db) or mysql_err('Не удается установить кодировку.','#'.mysql_errno().': '.mysql_error());
  }  
    
  private function str_sql ($sql) {
    return mysql_real_escape_string($sql);
  }
  private function mysql_err($txt,$error) {
    require(ROOT_DIR.'/messages/errors/error_db.php');  
    exit; 
}
  public function query($sql) {
    $result=mysql_query($sql) or $this->mysql_err('Не удается выполнить запрос к БД.','#'.mysql_errno().': '.mysql_error().'<br />--------------------------<br />SQL: '.$sql);
    return $result;
}
  
  public function count_rows($table,$where='') { //Метод подсчета количества строк в таблице.
    if($where!='') { $where=' WHERE '.$where; } 
    $result=$this->query('SELECT COUNT(1) FROM `'.$table.'`'.$where);
    return mysql_result($result,0);
  }
  
  public function inc($table,$data,$where='',$inc=1) {
    if($where!='') { $where=' WHERE '.$where; }
    $inc=(int)$inc;
    $query='`'.$data.'`=`'.$data.'`+'.$inc;
    $query='UPDATE  `'.$table.'` SET '.$query.' '.$where;
    $this->query($query);
    return mysql_affected_rows();   
  }
  
  public function dec($table,$data,$where='',$dec=1) {
    if($where!='') { $where=' WHERE '.$where; }
    $dec=(int)$dec;
    $query='`'.$data.'`=`'.$data.'`-'.$dec;
    $query='UPDATE  `'.$table.'` SET '.$query.' '.$where;
    $this->query($query);
    return mysql_affected_rows();   
  }
  
    public function insert_id() { //ID добавленной записи.
    $int=mysql_result($this->query('SELECT LAST_INSERT_ID()'),0);
    return $int;
  }
  
  public function select($table,$data='*',$where='') { //Метод запроса данных в таблице.
    if($where!='') { $where=' WHERE '.$where; }
    $query='SELECT  '.$data.' FROM `'.$table.'` '.$where;
    $result=$this->query($query);
    return $result;
  }
  
  public function insert($tabl,$data) {
     foreach ($data as $key=>$val) {
        $k[]='`'.$key.'`';
        $v[]='\''.$this->str_sql($val).'\'';
     }
     $k=implode(",",$k);
     $v=implode(",",$v);
     $query='INSERT INTO `'.$tabl.'` ('.$k.') VALUE ('.$v.')';
     $this->query($query);
     return mysql_affected_rows();   
    }
  
  public function update($table,$data,$where='') { //Метод обновления данных в таблице.
    if($where!='') { $where=' WHERE '.$where; }
    
    foreach ($data as $key=>$val) {
        $query[]='`'.$key.'`=\''.$this->str_sql($val).'\'';
    }                                                  
    $query=implode(',',$query);
    $query='UPDATE  `'.$table.'` SET '.$query.' '.$where;
    $this->query($query);