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
|
<?php /************************************************************************** * * * 4images - A Web Based Image Gallery Management System * * ---------------------------------------------------------------- * * * * File: db_mysql.php * * Copyright: (C) 2002 Jan Sorgalla * * Email: jan@4homepages.de * * Web: http://www.4homepages.de * * Scriptversion: 1.7.1 * * * * Never released without support from: Nicky (http://www.nicky.net) * * * ************************************************************************** * * * Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz- * * bedingungen (Lizenz.txt) für weitere Informationen. * * --------------------------------------------------------------- * * This script is NOT freeware! Please read the Copyright Notice * * (Licence.txt) for further information. * * * *************************************************************************/ if (!defined('ROOT_PATH')) { die("Security violation"); }
class Db { var $no_error = 0; var $connection; var $query_id = 0; var $query_count = 0; var $query_time = 0; var $query_array = array(); var $table_fields = array();
function Db($db_host, $db_user, $db_password = "", $db_name = "", $db_pconnect = 0) { $connect_handle = ($db_pconnect) ? "mysql_pconnect" : "mysql_connect"; if (!$this->connection = $connect_handle($db_host, $db_user, $db_password)) { $this->error("Could not connect to the database server ($db_host, $db_user).", 1); } if ($db_name != "") { if (!@mysql_select_db($db_name)) { @mysql_close($this->connection); $this->error("Could not select database ($db_name).", 1); } } return $this->connection; }
function close() { if ($this->connection) { if ($this->query_id) { @mysql_free_result($this->query_id); } return @mysql_close($this->connection); } else { return false; } }
function query($query = "") { unset($this->query_id); if ($query != "") { if ((defined("PRINT_QUERIES") && PRINT_QUERIES == 1) || (defined("PRINT_STATS") && PRINT_STATS == 1)) { $startsqltime = explode(" ", microtime()); } if (!$this->query_id = @mysql_query($query, $this->connection)) { $this->error("<b>Bad SQL Query</b>: ".htmlentities($query)."<br /><b>".mysql_error()."</b>"); } if ((defined("PRINT_QUERIES") && PRINT_QUERIES == 1) || (defined("PRINT_STATS") && PRINT_STATS == 1)) { $endsqltime = explode(" ", microtime()); $totalsqltime = round($endsqltime[0]-$startsqltime[0]+$endsqltime[1]-$startsqltime[1],3); $this->query_time += $totalsqltime; $this->query_count++; } if (defined("PRINT_QUERIES") && PRINT_QUERIES == 1) { $query_stats = htmlentities($query); $query_stats .= "<br><b>Querytime:</b> ".$totalsqltime; $this->query_array[] = $query_stats; } return $this->query_id; } }
function fetch_array($query_id = -1, $assoc = 0) { if ($query_id != -1) { $this->query_id = $query_id; } if ($this->query_id) { return ($assoc) ? mysql_fetch_assoc($this->query_id) : mysql_fetch_array($this->query_id); } }
function free_result($query_id = -1) { if ($query_id != -1) { $this->query_id = $query_id; } return @mysql_free_result($this->query_id); }
function query_firstrow($query = "") { if ($query != "") { $this->query($query); } $result = $this->fetch_array($this->query_id); $this->free_result(); return $result; }
function get_numrows($query_id = -1) { if ($query_id != -1) { $this->query_id = $query_id; } return mysql_num_rows($this->query_id); }
function get_insert_id() { return ($this->connection) ? @mysql_insert_id($this->connection) : 0; } function get_next_id($column = "", $table = "") { if (!empty($column) && !empty($table)) { $sql = "SELECT MAX($column) AS max_id FROM $table"; $row = $this->query_firstrow($sql); return (($row['max_id'] + 1) > 0) ? $row['max_id'] + 1 : 1; } else { return NULL; } }
function get_numfields($query_id = -1) { if ($query_id != -1) { $this->query_id = $query_id; } return @mysql_num_fields($this->query_id); }
function get_fieldname($query_id = -1, $offset) { if ($query_id != -1) { $this->query_id = $query_id; } return @mysql_field_name($this->query_id, $offset); }
function get_fieldtype($query_id = -1, $offset) { if ($query_id != -1) { $this->query_id = $query_id; } return @mysql_field_type($this->query_id, $offset); }
function affected_rows() { return ($this->connection) ? @mysql_affected_rows($this->connection) : 0; }
function is_empty($query = "") { if ($query != "") { $this->query($query); } return (!mysql_num_rows($this->query_id)) ? 1 : 0; }
function not_empty($query = "") { if ($query != "") { $this->query($query); } return (!mysql_num_rows($this->query_id)) ? 0 : 1; } function get_table_fields($table) { if (!empty($this->table_fields[$table])) { return $this->table_fields[$table]; } $this->table_fields[$table] = array(); $result = $this->query("SHOW FIELDS FROM $table"); while ($row = $this->fetch_array($result)) { $this->table_fields[$table][$row['Field']] = $row['Type']; } return $this->table_fields[$table]; }
function error($errmsg, $halt = 0) { if (!$this->no_error) { echo "<br /><font color='#FF0000'><b>DB Error</b></font>: ".$errmsg."<br />"; if ($halt) { exit; } } } } // end of class ?>
|