php class database public queryCount public queryTotalTime public quer

 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
<?php {
class database {
public $queryCount = 0;
public $queryTotalTime = 0;
public $queries = Array();
public $log;
protected $db;
function __construct() {
$this->MySQLiConnect();
}
protected function MySQLiConnect() {
$db = mysqli_connect("127.0.0.1", "root", "123456", "site0") or die("Error! " . mysqli_error($db));
$this->db = $db;
$this->query('SET names utf8');
}
public function fetch($res) {
return mysqli_fetch_assoc($res);
}
public function query($string) {
$this->queryCount++;
$startQueryTime = microtime(true);
$q = mysqli_query($this->db, $string);
$endQueryTime = microtime(true);
$this->queryTotalTime += $endQueryTime - $startQueryTime;
$this->queries[] = $string;
$this->log .= "\r\n[" . microtime(true) . "] " . $string . " - " . (round($endQueryTime - $startQueryTime) * 1000) . " мс.";
return $q;
}
public function insert_id() {
return mysqli_insert_id($this->db);
}
public function filter($string) {
if (gettype($string) == "string")
return mysqli_real_escape_string($this->db, $string);
else return $string;
}
public function num_rows($res) {
return mysqli_num_rows($res);
}
public function error() {
return mysqli_error($this->db);
}
protected function mysqli_result($res, $row = 0, $col = 0){
if (mysqli_num_rows($res) && $row <= (mysqli_num_rows($res)-1) && $row >= 0){
mysqli_data_seek($res,$row);
$resrow = mysqli_fetch_row($res);
if (isset($resrow[$col])){
return $resrow[$col];
}
}
return false;
}
public function result($res, $row) {
return $this->mysqli_result($res, $row);
}
public function getQueries() {
echo $this->queryCount . " SQL-запросов за " . round(($this->queryTotalTime) * 1000) . ' мс';
}
}
} ?>