-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabase.class.php
More file actions
242 lines (183 loc) · 5.14 KB
/
Database.class.php
File metadata and controls
242 lines (183 loc) · 5.14 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
*
* Copyright (c) 2003-2011 Ramon Antonio Parada <ramon@bigpress.net>
*
* Database class
*
* @category Database
* @package MyActiveRecord
* @author Ramon Antonio Parada <ramon@bigpress.net>
* @copyright 2011 Ramon Antonio Parada
* @version 0.5
*/
class Database {
var $link;
var $count = 0;
var $record;
var $result;
var $lasttime = 0;
var $totaltime = 0;
//anhadir el soporte para estas dos
var $debug = false;
//var $profiling = false;
/**
* Private function to create a new Database object
*/
function __construct() {
$this->link = @mysql_connect(SQL_HOST, SQL_LOGIN, SQL_PASSE);
// or Database::busy();
if ($this->link) {
if (!@mysql_select_db (SQL_DBASE)) {
$this->link = FALSE;
}
}
//or Database::busy();
}
function procesos() {
$db =& Database::getInstance();
$result = $db->query("show processlist;");
$count = $db->num_rows($result);
return $count;
}
function isbusy($limit = 5) {
return ((!$this->link) || ($this->procesos() > $limit));
}
function busy() {
$error ="Estamos sufriendo una sobrecarga\n";
$smarty =& Smarty::getInstance();
$smarty->display("error.tpl");
die ();
}
/**
* Retrieves an (unique) instance of the database handdler.
*/
function &getInstance() {
static $singleton;
if (!$singleton)
$singleton = new Database();
return $singleton;
}
/**
*
*/
function query($sql) {
//if ($this->profiling){
// $mysql_query('set profiling=1', $this->link);
//}
$begin = microtime( true );
$this->result = mysql_query($sql, $this->link);
$end = microtime( true );
$this->lasttime = $end - $begin;
$this->totaltime += $this->lasttime;
$this->count++;
//$this->thresh = 10;
//if( $this->thresh && ($end - $begin) >= $this->thresh ) error_log( ... );
//if ($this->profiling){
// mysql_query("select sum(duration) as qtime from information_schema.profiling where query_id=1", $this->link);
//}
if (!$this->result) {
// trigger_error("MyActiveRecord::Query() - query failed: $strSQL with error: ".$db->error(), E_USER_WARNING);
$this->sql_error($sql);
return false;
}
return $this->result;
}
/**
*
*/
function close () {
mysql_close($this->link);
}
/**
*
*/
function getCount() {
return $this->count;
}
function found_rows() {
$sql = "SELECT FOUND_ROWS();";
$result = $this->query($sql);
$item = $this->fetch_array($result, MYSQL_NUM);
return $item[0];
}
function fetch_array($query_id, $result_type= MYSQL_ASSOC ) {
$this->record = mysql_fetch_array($query_id,$result_type);
return $this->record;
}
function fetch_assoc($resource) {
$this->record = mysql_fetch_assoc($resource);
return $this->record;
}
function fetch_object($query_id, $class_name=null, $params=null) {
// $this->record = mysql_fetch_object($query_id);
if ($class_name == null)
return mysql_fetch_object($query_id);
return mysql_fetch_object($query_id, $class_name, $params);
}
function num_rows($query_id) {
return ($query_id) ? mysql_num_rows($query_id) : 0;
}
function num_fields($query_id) {
return ($query_id) ? mysql_num_fields($query_id) : 0;
}
function fetch_row($resource) {
return mysql_fetch_row($resource );
}
function free_result($query_id) {
return mysql_free_result($query_id);
}
/**
*@deprecated
*/
function real_escape_string($str) {
return mysql_real_escape_string($str);
}
function affected_rows() {
return mysql_affected_rows($this->link);
}
function insert_id() {
return mysql_insert_id();
}
function error() {
return $this->link?mysql_error($this->link):false;
}
/**
@deprecated
*/
function close_db() {
if($this->link) {
return mysql_close($this->link);
} else {
return false;
}
}
function escape_string($str) {
return mysql_real_escape_string($str, $this->link);
}
function sql_error($sql) {
$description = mysql_error($this->link);
$number = mysql_errno($this->link);
if ($number == "0") {
Database::busy();
}
$message = "Query Error";
$error ="MySQL Error : $message\n";
$error .="User : ".SQL_LOGIN."\n";
$error.="Error Number: $number $description\n";
$error.="SQL : $sql\n";
$error.="Date : ".date("D, F j, Y H:i:s")."\n";
$error.="IP : ".getenv("REMOTE_ADDR")."\n";
$error.="Browser : ".getenv("HTTP_USER_AGENT")."\n";
$error.="Referer : ".getenv("HTTP_REFERER")."\n";
$error.="PHP Version : ".PHP_VERSION."\n";
$error.="OS : ".PHP_OS."\n";
$error.="Server : ".getenv("SERVER_SOFTWARE")."\n";
$error.="Server Name : ".getenv("SERVER_NAME")."\n";
$error.="Script Name : ".getenv("SCRIPT_NAME")."\n";
echo $message;
echo $error;
request::log($message);
request::log($error);
}
}