Rev Author Line No. Line
130 kaklik 1 <?php
2 /***************************************************************************
3 * mysql4.php
4 * -------------------
5 * begin : Saturday, Feb 13, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : supportphpbb.com
8 *
9 * $Id: mysql4.php,v 1.5.2.1 2005/09/18 16:17:20 acydburn Exp $
10 *
11 ***************************************************************************/
12  
13 /***************************************************************************
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 ***************************************************************************/
21  
22 if(!defined("SQL_LAYER"))
23 {
24  
25 define("SQL_LAYER","mysql4");
26  
27 class sql_db
28 {
29  
30 var $db_connect_id;
31 var $query_result;
32 var $row = array();
33 var $rowset = array();
34 var $num_queries = 0;
35 var $in_transaction = 0;
36  
37 //
38 // Constructor
39 //
40 function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
41 {
42 $this->persistency = $persistency;
43 $this->user = $sqluser;
44 $this->password = $sqlpassword;
45 $this->server = $sqlserver;
46 $this->dbname = $database;
184 kaklik 47 mysql_query("SET NAMES 'utf8'");
130 kaklik 48  
49 $this->db_connect_id = ($this->persistency) ? mysql_pconnect($this->server, $this->user, $this->password) : mysql_connect($this->server, $this->user, $this->password);
50  
51 if( $this->db_connect_id )
52 {
53 if( $database != "" )
54 {
55 $this->dbname = $database;
56 $dbselect = mysql_select_db($this->dbname);
57  
58 if( !$dbselect )
59 {
60 mysql_close($this->db_connect_id);
61 $this->db_connect_id = $dbselect;
62 }
63 }
64  
65 return $this->db_connect_id;
66 }
67 else
68 {
69 return false;
70 }
71 }
72  
73 //
74 // Other base methods
75 //
76 function sql_close()
77 {
78 if( $this->db_connect_id )
79 {
80 //
81 // Commit any remaining transactions
82 //
83 if( $this->in_transaction )
84 {
85 mysql_query("COMMIT", $this->db_connect_id);
86 }
87  
88 return mysql_close($this->db_connect_id);
89 }
90 else
91 {
92 return false;
93 }
94 }
95  
96 //
97 // Base query method
98 //
99 function sql_query($query = "", $transaction = FALSE)
100 {
101 //
102 // Remove any pre-existing queries
103 //
104 unset($this->query_result);
105  
106 if( $query != "" )
107 {
108 $this->num_queries++;
109 if( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )
110 {
111 $result = mysql_query("BEGIN", $this->db_connect_id);
112 if(!$result)
113 {
114 return false;
115 }
116 $this->in_transaction = TRUE;
117 }
118  
119 $this->query_result = mysql_query($query, $this->db_connect_id);
120 }
121 else
122 {
123 if( $transaction == END_TRANSACTION && $this->in_transaction )
124 {
125 $result = mysql_query("COMMIT", $this->db_connect_id);
126 }
127 }
128  
129 if( $this->query_result )
130 {
131 unset($this->row[$this->query_result]);
132 unset($this->rowset[$this->query_result]);
133  
134 if( $transaction == END_TRANSACTION && $this->in_transaction )
135 {
136 $this->in_transaction = FALSE;
137  
138 if ( !mysql_query("COMMIT", $this->db_connect_id) )
139 {
140 mysql_query("ROLLBACK", $this->db_connect_id);
141 return false;
142 }
143 }
144  
145 return $this->query_result;
146 }
147 else
148 {
149 if( $this->in_transaction )
150 {
151 mysql_query("ROLLBACK", $this->db_connect_id);
152 $this->in_transaction = FALSE;
153 }
154 return false;
155 }
156 }
157  
158 //
159 // Other query methods
160 //
161 function sql_numrows($query_id = 0)
162 {
163 if( !$query_id )
164 {
165 $query_id = $this->query_result;
166 }
167  
168 return ( $query_id ) ? mysql_num_rows($query_id) : false;
169 }
170  
171 function sql_affectedrows()
172 {
173 return ( $this->db_connect_id ) ? mysql_affected_rows($this->db_connect_id) : false;
174 }
175  
176 function sql_numfields($query_id = 0)
177 {
178 if( !$query_id )
179 {
180 $query_id = $this->query_result;
181 }
182  
183 return ( $query_id ) ? mysql_num_fields($query_id) : false;
184 }
185  
186 function sql_fieldname($offset, $query_id = 0)
187 {
188 if( !$query_id )
189 {
190 $query_id = $this->query_result;
191 }
192  
193 return ( $query_id ) ? mysql_field_name($query_id, $offset) : false;
194 }
195  
196 function sql_fieldtype($offset, $query_id = 0)
197 {
198 if( !$query_id )
199 {
200 $query_id = $this->query_result;
201 }
202  
203 return ( $query_id ) ? mysql_field_type($query_id, $offset) : false;
204 }
205  
206 function sql_fetchrow($query_id = 0)
207 {
208 if( !$query_id )
209 {
210 $query_id = $this->query_result;
211 }
212  
213 if( $query_id )
214 {
215 $this->row[$query_id] = mysql_fetch_array($query_id, MYSQL_ASSOC);
216 return $this->row[$query_id];
217 }
218 else
219 {
220 return false;
221 }
222 }
223  
224 function sql_fetchrowset($query_id = 0)
225 {
226 if( !$query_id )
227 {
228 $query_id = $this->query_result;
229 }
230  
231 if( $query_id )
232 {
233 unset($this->rowset[$query_id]);
234 unset($this->row[$query_id]);
235  
236 while($this->rowset[$query_id] = mysql_fetch_array($query_id, MYSQL_ASSOC))
237 {
238 $result[] = $this->rowset[$query_id];
239 }
240  
241 return $result;
242 }
243 else
244 {
245 return false;
246 }
247 }
248  
249 function sql_fetchfield($field, $rownum = -1, $query_id = 0)
250 {
251 if( !$query_id )
252 {
253 $query_id = $this->query_result;
254 }
255  
256 if( $query_id )
257 {
258 if( $rownum > -1 )
259 {
260 $result = mysql_result($query_id, $rownum, $field);
261 }
262 else
263 {
264 if( empty($this->row[$query_id]) && empty($this->rowset[$query_id]) )
265 {
266 if( $this->sql_fetchrow() )
267 {
268 $result = $this->row[$query_id][$field];
269 }
270 }
271 else
272 {
273 if( $this->rowset[$query_id] )
274 {
275 $result = $this->rowset[$query_id][0][$field];
276 }
277 else if( $this->row[$query_id] )
278 {
279 $result = $this->row[$query_id][$field];
280 }
281 }
282 }
283  
284 return $result;
285 }
286 else
287 {
288 return false;
289 }
290 }
291  
292 function sql_rowseek($rownum, $query_id = 0)
293 {
294 if( !$query_id )
295 {
296 $query_id = $this->query_result;
297 }
298  
299 return ( $query_id ) ? mysql_data_seek($query_id, $rownum) : false;
300 }
301  
302 function sql_nextid()
303 {
304 return ( $this->db_connect_id ) ? mysql_insert_id($this->db_connect_id) : false;
305 }
306  
307 function sql_freeresult($query_id = 0)
308 {
309 if( !$query_id )
310 {
311 $query_id = $this->query_result;
312 }
313  
314 if ( $query_id )
315 {
316 unset($this->row[$query_id]);
317 unset($this->rowset[$query_id]);
318  
319 mysql_free_result($query_id);
320  
321 return true;
322 }
323 else
324 {
325 return false;
326 }
327 }
328  
329 function sql_error()
330 {
331 $result['message'] = mysql_error($this->db_connect_id);
332 $result['code'] = mysql_errno($this->db_connect_id);
333  
334 return $result;
335 }
336  
337 } // class sql_db
338  
339 } // if ... define
340  
341 ?>