Delineating functions
[isso.git] / db_mysql.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * MySQL Database Abstraction Layer
24 * db_mysql.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * MySQL Database Abstraction Layer
31 *
32 * This framework is a function wrapper for MySQL functions so we can have
33 * better error reporting and query reporting.
34 *
35 * @author Iris Studios, Inc.
36 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
37 * @version $Revision$
38 * @package ISSO
39 *
40 */
41 class DB_MySQL
42 {
43 /**
44 * Framework registry object
45 * @var object
46 */
47 var $registry = null;
48
49 /**
50 * Database that is used in the application
51 * @var string
52 */
53 var $database = '';
54
55 /**
56 * Determines whether or not errors should be shown
57 * @var bool
58 */
59 var $errshow = true;
60
61 /**
62 * Current error number
63 * @var integer
64 */
65 var $error_no = 0;
66
67 /**
68 * Description of current error
69 * @var string
70 */
71 var $error_str = '';
72
73 /**
74 * Currend open MySQL connexion
75 * @var resource
76 */
77 var $link_id = null;
78
79 /**
80 * Current query ID
81 * @var integer
82 */
83 var $query_id = null;
84
85 /**
86 * Current query string
87 * @var string
88 */
89 var $query_str = '';
90
91 /**
92 * History of all executed queryies
93 * @var array
94 */
95 var $history = array();
96
97 // ###################################################################
98 /**
99 * Constructor
100 */
101 function __construct(&$registry)
102 {
103 $this->registry =& $registry;
104 }
105
106 // ###################################################################
107 /**
108 * (PHP 4) Constructor
109 */
110 function DB_MySQL(&$registry)
111 {
112 $this->__construct($registry);
113 }
114
115 // ###################################################################
116 /**
117 * Connect to a the specified database
118 *
119 * @access public
120 *
121 * @param string Server name
122 * @param string User name
123 * @param string Password
124 * @param bool Use p-connect?
125 *
126 * @return bool Result of connect
127 */
128 function connect($server, $user, $password, $pconnect)
129 {
130 if ($this->link_id == 0)
131 {
132 if ($pconnect)
133 {
134 $this->link_id = @mysql_pconnect($server, $user, $password);
135 }
136 else
137 {
138 $this->link_id = @mysql_connect($server, $user, $password);
139 }
140
141 if ($this->link_id == false)
142 {
143 $this->error('Link-Id == false, cannot connect');
144 return false;
145 }
146
147 if (@mysql_select_db($this->database, $this->link_id))
148 {
149 return true;
150 }
151 else
152 {
153 $this->error('Cannot use the database \'' . $this->database . '\'');
154 return false;
155 }
156 }
157 }
158
159 // ###################################################################
160 /**
161 * Send a query to MySQL
162 *
163 * @access public
164 *
165 * @param string Query string
166 *
167 * @return integer Resource ID for query
168 */
169 function query($query_str)
170 {
171 $this->query_id = @mysql_query($query_str, $this->link_id);
172 $this->query_str = $query_str;
173 $this->history[] = $this->query_str;
174
175 if (defined('ISSO_SHOW_QUERIES_LIVE'))
176 {
177 if (constant('ISSO_SHOW_QUERIES_LIVE'))
178 {
179 print($this->query_str . '<hr />');
180 }
181 }
182
183 if (!$this->query_id)
184 {
185 $this->error('Invalid SQL query');
186 }
187
188 return $this->query_id;
189 }
190
191 // ###################################################################
192 /**
193 * Escape a string depending on character set
194 *
195 * @access public
196 *
197 * @param string String to be escaped
198 *
199 * @return string Escaped string
200 */
201 function escape_string($string)
202 {
203 return @mysql_real_escape_string($string, $this->link_id);
204 }
205
206 // ###################################################################
207 /**
208 * Fetch the query result as an array
209 *
210 * @access public
211 *
212 * @param integer Query ID
213 *
214 * @return array A row of the query result
215 */
216 function fetch_array($query_id)
217 {
218 return @mysql_fetch_array($query_id, MYSQL_ASSOC);
219 }
220
221 // ###################################################################
222 /**
223 * Fetch the query result as an object
224 *
225 * @access public
226 *
227 * @param integer Query ID
228 *
229 * @return object An object with the query result
230 */
231 function fetch_object($query_id)
232 {
233 return @mysql_fetch_object($query_id);
234 }
235
236 // ###################################################################
237 /**
238 * Free the current MySQL query result
239 *
240 * @access public
241 *
242 * @param integer Query ID
243 */
244 function free_result($query_id)
245 {
246 @mysql_free_result($query_id);
247 $this->query_id = 0;
248 $this->query_str = '';
249 }
250
251 // ###################################################################
252 /**
253 * Send a MySQL query and return the first row of the results
254 *
255 * @access public
256 *
257 * @param string Query string
258 * @param string Result return function (in the database layer)
259 *
260 * @return mixed Results in variable formats
261 */
262 function query_first($query_str, $callback = 'fetch_array')
263 {
264 $resource = $this->query($query_str);
265 $return = $this->$callback($resource);
266 $this->free_result($resource);
267 return $return;
268 }
269
270 // ###################################################################
271 /**
272 * Fetch the unique ID of the record just inserted
273 *
274 * @access public
275 *
276 * @return integer MySQL insert ID
277 */
278 function insert_id()
279 {
280 return @mysql_insert_id($this->link_id);
281 }
282
283 // ###################################################################
284 /**
285 * Fetch the number of rows in the result
286 *
287 * @access public
288 *
289 * @param integer Query ID
290 *
291 * @return integer Number of MySQL rows
292 */
293 function num_rows($query_id)
294 {
295 return @mysql_num_rows($query_id);
296 }
297
298 // ###################################################################
299 /**
300 * Fetch the number of rows affected by the query
301 *
302 * @access public
303 *
304 * @param integer Query ID
305 *
306 * @return integer Number of affected rows
307 */
308 function affected_rows($query_id)
309 {
310 return @mysql_affected_rows($this->link_id);
311 }
312
313 // ###################################################################
314 /**
315 * MySQL error wrapper for ISSO::_message()
316 *
317 * @access protected
318 *
319 * @param string User defined error message
320 */
321 function error($message)
322 {
323 if ($this->errshow)
324 {
325 if ($this->link_id)
326 {
327 $this->error_no = mysql_errno($this->link_id);
328 $this->error_str = mysql_error($this->link_id);
329 }
330
331 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
332
333 $message_prepped = "<blockquote>\n<p>";
334 $message_prepped .= "\n\t&raquo; <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->query_str) ."</pre>\n<br />";
335 $message_prepped .= "\n\t&raquo; <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->error_no . "</span>\n<br />";
336 $message_prepped .= "\n\t&raquo; <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->error_str . "</span>\n<br />";
337 $message_prepped .= "\n\t&raquo; <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
338 $message_prepped .= "\n\t&raquo; <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
339 $message_prepped .= "\n</p>\n</blockquote>";
340
341 $this->registry->message('Database Error in `<em>' . $this->registry->application . '</em>`', $message_prepped, 3);
342 exit;
343 }
344 }
345 }
346
347 /*=====================================================================*\
348 || ###################################################################
349 || # $HeadURL$
350 || # $Id$
351 || ###################################################################
352 \*=====================================================================*/
353 ?>