php class DB static link static add_success_queries_ to_log false stat

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
class DB{
static $link;
static $add_success_queries_to_log=false;
static $queries=array(array('',0));
static $tmr=array();
static $num_of_queries=0;
static $mysql = array(); //настройки для подключения host,login,password,database,encode
static $connected=false;
private static function add_msg($str, $type='ok'){
if (self::$add_success_queries_to_log || $type!='ok'){
logger::AddEvent($str,$type);
}
}
static function Disconnect(){
mysql_close();
}
static function Connect($arr=false){ if (self::$connected) return true;
if(is_array($arr)) DB::$mysql=$arr;
if(mysql_pconnect(DB::$mysql['host'], DB::$mysql['login'], DB::$mysql['password'])){
self::add_msg("Success connect to MySQL",'ok');
}
else{
self::add_msg("Could not connect to MySQL (Error #".mysql_errno().": ".mysql_error().")",'err');
return false;
}
if(mysql_select_db (DB::$mysql['database'])){
self::add_msg("Success select database", 'ok');
}
else{
self::add_msg("Could not select database (Error #".mysql_errno().": ".mysql_error().")" ,'err');
return false;
}
self::$connected=true;
if (isset(DB::$mysql['encode'])&&DB::$mysql['encode']){
if (self::Query("SET NAMES '".DB::$mysql['encode']."';")==false){
self::add_msg("Error! #".mysql_errno().": ".mysql_error(),'err');
}
else {
self::add_msg("Success select encode \"".DB::$mysql['encode']."\"!",'ok');
}
}
return true;
}
static function Query($query){
//mysql_connect
if(!self::$connected && !self::Connect())
self::add_msg('Нет соединения с БД','err');
self::$num_of_queries++;
self::timer_start();
self::$link = mysql_query($query);
$time=self::timer_stop();
self::$queries[self::$num_of_queries]=array($query,$time);
self::$queries[0][1]+=$time;
if (mysql_errno()){
self::add_msg("Error #".mysql_errno().": ".mysql_error().". Query: \"".htmlspecialchars($query)."\"",'lag');
}
else{
self::add_msg("Success Query: \"".htmlspecialchars($query)."\"",'ok');
}
return (self::$link);
}
static function Fast_Arr_Query($query){
self::Query($query);
return self::GetAssocArray();
}
static function Fast_Row_Query($query){
self::Query($query);
return self::GetAssocRow();
}
static function Fast_Query($query){
self::Query($query);
$tmp=self::GetArray();
if (count($tmp)==1){
$tmp=$tmp[0];
}
elseif(count($tmp)>1){
foreach($tmp as $k=>$v){
if (count($v)==1){
$tmp[$k]=$v[0];
}
}
}
if (count($tmp)==1){$tmp=$tmp[0];}
return $tmp;
}
static function Fast_Query_Arr($query){
self::Query($query);
$tmp=self::GetArray();
foreach($tmp as $k=>$v){
if (count($v)==1){
$tmp[$k]=$v[0];
}
}
return $tmp;
}
static function GetRow($type=MYSQL_NUM){
if(mysql_num_rows(self::$link)){
return mysql_fetch_array(self::$link,$type);;
}
else{
return false;
}
}
static function GetAssocRow(){
return self::GetRow(MYSQL_ASSOC);
}
static function GetArray($type=MYSQL_NUM){
if(@mysql_num_rows(self::$link)){
$tmp=array();
while($row=self::GetRow($type)){
$tmp[]=$row;
}
return $tmp;
}
else
return false;
}
static function GetAssocArray($type=MYSQL_NUM){
return self::GetArray(MYSQL_ASSOC);
}
static function EscapeString($str){
return mysql_escape_string($str);
}
static function EscapeArray($arr){
if (is_array($arr)){
foreach($arr as $k=>$v){
$arr[$k]=DB::EscapeArray($arr[$k]);
}
}
else {
$arr = DB::EscapeString($arr);
}
return $arr;
}
static function timer_start(){
self::$tmr[]=microtime(true); }
static function timer_stop(){
return round((microtime(true)-array_pop(self::$tmr)),4); }
}
?>