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