Making everything properly documented so that we don't produce warnings when running...
[isso.git] / db_mysql.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 /**
14 * MySQL Database Abstraction Layer
15 * db_mysql.php
16 *
17 * @package ISSO
18 */
19
20 $OBJECT = 'MySQL Database Abstraction Layer';
21 $CLASS = 'MySQL_Database_Driver';
22 $OBJ = 'db';
23
24 /**
25 * MySQL Database Abstraction Layer
26 *
27 * This framework is a function wrapper for MySQL functions so we can have
28 * better error reporting and query reporting.
29 *
30 * @author Iris Studios, Inc.
31 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
32 * @version $Revision$
33 * @package ISSO
34 *
35 */
36 class MySQL_Database_Driver
37 {
38 /**
39 * Database that is used in the application
40 * @var str
41 * @see connect()
42 */
43 var $database = '';
44
45 /**
46 * Determines whether or not errors should be shown
47 * @var bool
48 * @see error()
49 */
50 var $errshow = true;
51
52 /**
53 * Current error number
54 * @var int
55 * @see error()
56 */
57 var $error_no = 0;
58
59 /**
60 * Description of current error
61 * @var str
62 * @see error()
63 */
64 var $error_str = '';
65
66 /**
67 * Currend open MySQL connexion
68 * @var res
69 * @see connect()
70 */
71 var $link_id = null;
72
73 /**
74 * Current query ID
75 * @var res
76 * @see query()
77 */
78 var $query_id = null;
79
80 /**
81 * Current query string
82 * @var str
83 * @see query()
84 */
85 var $query_str = '';
86
87 /**
88 * History of all executed queryies
89 * @var array
90 */
91 var $history = array();
92
93 /**
94 * Connect to a the specified database
95 *
96 * @param str Server name
97 * @param str User name
98 * @param str Password
99 * @param bool Use p-connect?
100 *
101 * @return bool Result of connect
102 */
103 function connect($server, $user, $password, $pconnect)
104 {
105 if ($this->link_id == 0)
106 {
107 if ($pconnect)
108 {
109 $this->link_id = @mysql_pconnect($server, $user, $password);
110 }
111 else
112 {
113 $this->link_id = @mysql_connect($server, $user, $password);
114 }
115
116 if ($this->link_id == false)
117 {
118 $this->error('Link-Id == false, cannot connect');
119 return false;
120 }
121
122 if (@mysql_select_db($this->database, $this->link_id))
123 {
124 return true;
125 }
126 else
127 {
128 $this->error('Cannot use the database \'' . $this->database . '\'');
129 return false;
130 }
131 }
132 }
133
134 /**
135 * Send a query to MySQL
136 *
137 * @param str Query string
138 *
139 * @return res Resource ID for query
140 */
141 function query($query_str)
142 {
143 $this->query_id = @mysql_query($query_str, $this->link_id);
144 $this->query_str = $query_str;
145 $this->history[] = $this->query_str;
146
147 if (defined('ISSO_SHOW_QUERIES_LIVE'))
148 {
149 if (constant('ISSO_SHOW_QUERIES_LIVE'))
150 {
151 print($this->query_str . '<hr />');
152 }
153 }
154
155 if (!$this->query_id)
156 {
157 $this->error('Invalid SQL query');
158 }
159
160 return $this->query_id;
161 }
162
163 /**
164 * Escape a string depending on character set
165 *
166 * @param str String to be escaped
167 *
168 * @return str Escaped string
169 */
170 function escape_string($string)
171 {
172 return @mysql_real_escape_string($string, $this->link_id);
173 }
174
175 /**
176 * Fetch the query result as an array
177 *
178 * @param res Query ID
179 *
180 * @return array A row of the query result
181 */
182 function fetch_array($query_id)
183 {
184 return @mysql_fetch_array($query_id, MYSQL_ASSOC);
185 }
186
187 /**
188 * Fetch the query result as an object
189 *
190 * @param res Query ID
191 *
192 * @return obj An object with the query result
193 */
194 function fetch_object($query_id)
195 {
196 return @mysql_fetch_object($query_id);
197 }
198
199 /**
200 * Free the current MySQL query result
201 *
202 * @param res Query ID
203 */
204 function free_result($query_id)
205 {
206 @mysql_free_result($query_id);
207 $this->query_id = 0;
208 $this->query_str = '';
209 }
210
211 /**
212 * Send a MySQL query and return the first row of the results
213 *
214 * @param str Query string
215 * @param str Result return function (in the database layer)
216 *
217 * @return mixed Results in variable formats
218 */
219 function query_first($query_str, $callback = 'fetch_array')
220 {
221 $resource = $this->query($query_str);
222 $return = $this->$callback($resource);
223 $this->free_result($resource);
224 return $return;
225 }
226
227 /**
228 * Fetch the unique ID of the record just inserted
229 *
230 * @return int MySQL insert ID
231 */
232 function insert_id()
233 {
234 return @mysql_insert_id($this->link_id);
235 }
236
237 /**
238 * Fetch the number of rows in the result
239 *
240 * @param res Query ID
241 *
242 * @return int Number of MySQL rows
243 */
244 function num_rows($query_id)
245 {
246 return @mysql_num_rows($query_id);
247 }
248
249 /**
250 * Fetch the number of rows affected by the query
251 *
252 * @param res Query ID
253 *
254 * @return int Number of affected rows
255 */
256 function affected_rows($query_id)
257 {
258 return @mysql_affected_rows($this->link_id);
259 }
260
261 /**
262 * MySQL error wrapper for ISSO::_message()
263 *
264 * @param str User defined error message
265 */
266 function error($message)
267 {
268 global $_isso;
269
270 if ($this->errshow)
271 {
272 if ($this->link_id)
273 {
274 $this->error_no = mysql_error_no($this->link_id);
275 $this->error_str = mysql_error($this->link_id);
276 }
277
278 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
279
280 $message_prepped = "<blockquote>\n<p>";
281 $message_prepped .= "\n\t&raquo; <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . $this->query_str ."</pre>\n<br />";
282 $message_prepped .= "\n\t&raquo; <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->error_no . "</span>\n<br />";
283 $message_prepped .= "\n\t&raquo; <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->error_str . "</span>\n<br />";
284 $message_prepped .= "\n\t&raquo; <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
285 $message_prepped .= "\n\t&raquo; <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
286 $message_prepped .= "\n</p>\n</blockquote>";
287
288 $_isso->_message('Database Error in `<em>' . $_isso->application . '</em>`', $message_prepped, 3);
289 exit;
290 }
291 }
292 }
293
294 /*=====================================================================*\
295 || ###################################################################
296 || # $HeadURL$
297 || # $Id$
298 || ###################################################################
299 \*=====================================================================*/
300 ?>