1 | <?php |
---|
2 | |
---|
3 | class DBConnection |
---|
4 | { |
---|
5 | function DBConnection() |
---|
6 | { |
---|
7 | static $conn_id; |
---|
8 | |
---|
9 | if (is_resource($conn_id)) |
---|
10 | { |
---|
11 | $this->id_ = $conn_id; |
---|
12 | return; |
---|
13 | } |
---|
14 | if (!$this->id_ = mysql_connect(DB_HOST, DB_USER, DB_PASS)) |
---|
15 | die("Unable to connect to mysql database."); |
---|
16 | |
---|
17 | mysql_select_db(DB_NAME); |
---|
18 | $conn_id = $this->id_; |
---|
19 | } |
---|
20 | |
---|
21 | function id() |
---|
22 | { |
---|
23 | return $this->id_; |
---|
24 | } |
---|
25 | |
---|
26 | function affectedRows() |
---|
27 | { |
---|
28 | return mysql_affected_rows($this->id_); |
---|
29 | } |
---|
30 | } |
---|
31 | |
---|
32 | class DBQuery |
---|
33 | { |
---|
34 | function DBQuery() |
---|
35 | { |
---|
36 | if (DB_USE_QCACHE === true) |
---|
37 | { |
---|
38 | $this = new DBCachedQuery(); |
---|
39 | } |
---|
40 | else |
---|
41 | { |
---|
42 | $this = new DBNormalQuery(); |
---|
43 | } |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | class DBNormalQuery |
---|
48 | { |
---|
49 | function DBNormalQuery() |
---|
50 | { |
---|
51 | $this->executed_ = false; |
---|
52 | $this->dbconn_ = new DBConnection; |
---|
53 | } |
---|
54 | |
---|
55 | function queryCount($increase = false) |
---|
56 | { |
---|
57 | static $count; |
---|
58 | |
---|
59 | if ($increase) |
---|
60 | { |
---|
61 | $count++; |
---|
62 | } |
---|
63 | |
---|
64 | return $count; |
---|
65 | } |
---|
66 | |
---|
67 | function execute($sql) |
---|
68 | { |
---|
69 | $t1 = strtok(microtime(), ' ') + strtok(''); |
---|
70 | |
---|
71 | $this->resid_ = mysql_query($sql, $this->dbconn_->id()); |
---|
72 | |
---|
73 | if ($this->resid_ == false) |
---|
74 | { |
---|
75 | if (defined('DB_HALTONERROR') && DB_HALTONERROR) |
---|
76 | { |
---|
77 | echo "Database error: " . mysql_error($this->dbconn_->id()) . "<br>"; |
---|
78 | echo "SQL: " . $sql . "<br>"; |
---|
79 | exit; |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | return false; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | $this->exectime_ = strtok(microtime(), ' ') + strtok('') - $t1; |
---|
88 | $this->executed_ = true; |
---|
89 | |
---|
90 | if (KB_PROFILE == 2) |
---|
91 | { |
---|
92 | file_put_contents('/tmp/profile.lst', $sql . "\nExecution time: " . $this->exectime_ . "\n", FILE_APPEND); |
---|
93 | } |
---|
94 | |
---|
95 | $this->queryCount(true); |
---|
96 | |
---|
97 | return true; |
---|
98 | } |
---|
99 | |
---|
100 | function recordCount() |
---|
101 | { |
---|
102 | return mysql_num_rows($this->resid_); |
---|
103 | } |
---|
104 | |
---|
105 | function getRow() |
---|
106 | { |
---|
107 | if ($this->resid_) |
---|
108 | { |
---|
109 | return mysql_fetch_assoc($this->resid_); |
---|
110 | } |
---|
111 | return false; |
---|
112 | } |
---|
113 | |
---|
114 | function rewind() |
---|
115 | { |
---|
116 | @mysql_data_seek($this->resid_, 0); |
---|
117 | } |
---|
118 | |
---|
119 | function getInsertID() |
---|
120 | { |
---|
121 | return mysql_insert_id(); |
---|
122 | } |
---|
123 | |
---|
124 | function execTime() |
---|
125 | { |
---|
126 | return $this->exectime_; |
---|
127 | } |
---|
128 | |
---|
129 | function executed() |
---|
130 | { |
---|
131 | return $this->executed_; |
---|
132 | } |
---|
133 | |
---|
134 | function getErrorMsg() |
---|
135 | { |
---|
136 | $msg = $this->sql_ . "<br>"; |
---|
137 | $msg .= "Query failed. " . mysql_error($this->dbconn_->id()); |
---|
138 | |
---|
139 | return $msg; |
---|
140 | } |
---|
141 | } |
---|
142 | ?> |
---|