-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbm.class.php
150 lines (124 loc) · 2.84 KB
/
dbm.class.php
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
<?php
/**
* This is a MySQL handler class
*
* @version 1.0
* @author PhpToys
* @copyright PhpToys 2007
*
*/
class DBM{
var $connection = '';
var $queryCounter = 0;
var $totalTime = 0;
var $errorCode = 0;
var $errorMsg = '';
var $resultSet = '';
/**
* Constuctor of the class.
* Connects to the server and selects the database.
*
* @param string $host
* @param string $user
* @param string $pass
* @param string $db
* @return DBManager
*/
function DBManager($host, $user, $pass, $db){
$startTime = $this->getMicroTime();
// Try to make a connection to the server
if (!$this->connection = @mysql_connect($host,$user,$pass,true)){
$this->errorCode = mysql_errno();
$this->errorMsg = mysql_error();
return false;
}
// Now select the database
if (!@mysql_select_db($db,$this->connection)){
$this->errorCode = mysql_errno();
$this->errorMsg = mysql_error();
@mysql_close($this->connection);
return false;
}
$this->totalTime += $this->getMicroTime() - $startTime;
return true;
}
/**
* Execute the sql statement and returns with the result set.
*
* @param string $sql
* @return unknown
*/
function executeQuery($sql){
$startTime = $this->getMicroTime();
++$this->queryCounter;
if(!$this->resultSet = @mysql_query($sql,$this->connection)){
$this->errorCode = mysql_errno();
$this->errorMsg = mysql_error();
$this->totalTime = $this->getMicroTime() - $startTime;
return false;
}
$this->totalTime += $this->getMicroTime() - $startTime;
return $this->resultSet;
}
/**
* This function loads the previous query result into an array.
*
* @return array
*/
function loadResult() {
$array = array();
while ($row = mysql_fetch_array( $this->resultSet )) {
$array[] = $row;
}
mysql_free_result( $this->resultSet );
return $array;
}
/**
* Returns with the number of selected rows in the previous sql statement.
*
* @return int
*/
function getSelectedRows()
{
return @mysql_num_rows($this->resultSet);
}
/**
* Returns with the number of the affected rows in the previous sql statement.
*
* @return int
*/
function getAffectedRows()
{
return @mysql_affected_rows($this->connection);
}
/**
* Get the ID generated from the previous INSERT operation
*
* @return int
*/
function getInsertId(){
return @mysql_insert_id($this->connection);
}
/**
* Return the total time spended in this class
*
* @return float
*/
function getDBTime(){
return round($this->totalTime,6);
}
function getSqlCount(){
return $this->queryCounter;
}
function getErrrorCode(){
return $this->errorCode;
}
function getErrorMessage(){
return $this->errorMsg;
}
function getMicroTime() {
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
}
?>