Removed 3 $SOMETHING variables at the top of modules; used in 1.0.x-style instantiation
[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 * Constructor
99 */
100 function DB_MySQL(&$registry)
101 {
102 $this->registry =& $registry;
103 }
104
105 /**
106 * Connect to a the specified database
107 *
108 * @param string Server name
109 * @param string User name
110 * @param string Password
111 * @param bool Use p-connect?
112 *
113 * @return bool Result of connect
114 */
115 function connect($server, $user, $password, $pconnect)
116 {
117 if ($this->link_id == 0)
118 {
119 if ($pconnect)
120 {
121 $this->link_id = @mysql_pconnect($server, $user, $password);
122 }
123 else
124 {
125 $this->link_id = @mysql_connect($server, $user, $password);
126 }
127
128 if ($this->link_id == false)
129 {
130 $this->error('Link-Id == false, cannot connect');
131 return false;
132 }
133
134 if (@mysql_select_db($this->database, $this->link_id))
135 {
136 return true;
137 }
138 else
139 {
140 $this->error('Cannot use the database \'' . $this->database . '\'');
141 return false;
142 }
143 }
144 }
145
146 /**
147 * Send a query to MySQL
148 *
149 * @param string Query string
150 *
151 * @return integer Resource ID for query
152 */
153 function query($query_str)
154 {
155 $this->query_id = @mysql_query($query_str, $this->link_id);
156 $this->query_str = $query_str;
157 $this->history[] = $this->query_str;
158
159 if (defined('ISSO_SHOW_QUERIES_LIVE'))
160 {
161 if (constant('ISSO_SHOW_QUERIES_LIVE'))
162 {
163 print($this->query_str . '<hr />');
164 }
165 }
166
167 if (!$this->query_id)
168 {
169 $this->error('Invalid SQL query');
170 }
171
172 return $this->query_id;
173 }
174
175 /**
176 * Escape a string depending on character set
177 *
178 * @param string String to be escaped
179 *
180 * @return string Escaped string
181 */
182 function escape_string($string)
183 {
184 return @mysql_real_escape_string($string, $this->link_id);
185 }
186
187 /**
188 * Fetch the query result as an array
189 *
190 * @param integer Query ID
191 *
192 * @return array A row of the query result
193 */
194 function fetch_array($query_id)
195 {
196 return @mysql_fetch_array($query_id, MYSQL_ASSOC);
197 }
198
199 /**
200 * Fetch the query result as an object
201 *
202 * @param integer Query ID
203 *
204 * @return object An object with the query result
205 */
206 function fetch_object($query_id)
207 {
208 return @mysql_fetch_object($query_id);
209 }
210
211 /**
212 * Free the current MySQL query result
213 *
214 * @param integer Query ID
215 */
216 function free_result($query_id)
217 {
218 @mysql_free_result($query_id);
219 $this->query_id = 0;
220 $this->query_str = '';
221 }
222
223 /**
224 * Send a MySQL query and return the first row of the results
225 *
226 * @param string Query string
227 * @param string Result return function (in the database layer)
228 *
229 * @return mixed Results in variable formats
230 */
231 function query_first($query_str, $callback = 'fetch_array')
232 {
233 $resource = $this->query($query_str);
234 $return = $this->$callback($resource);
235 $this->free_result($resource);
236 return $return;
237 }
238
239 /**
240 * Fetch the unique ID of the record just inserted
241 *
242 * @return integer MySQL insert ID
243 */
244 function insert_id()
245 {
246 return @mysql_insert_id($this->link_id);
247 }
248
249 /**
250 * Fetch the number of rows in the result
251 *
252 * @param integer Query ID
253 *
254 * @return integer Number of MySQL rows
255 */
256 function num_rows($query_id)
257 {
258 return @mysql_num_rows($query_id);
259 }
260
261 /**
262 * Fetch the number of rows affected by the query
263 *
264 * @param integer Query ID
265 *
266 * @return integer Number of affected rows
267 */
268 function affected_rows($query_id)
269 {
270 return @mysql_affected_rows($this->link_id);
271 }
272
273 /**
274 * MySQL error wrapper for ISSO::_message()
275 *
276 * @param string User defined error message
277 */
278 function error($message)
279 {
280 if ($this->errshow)
281 {
282 if ($this->link_id)
283 {
284 $this->error_no = mysql_errno($this->link_id);
285 $this->error_str = mysql_error($this->link_id);
286 }
287
288 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
289
290 $message_prepped = "<blockquote>\n<p>";
291 $message_prepped .= "\n\t&raquo; <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . $this->query_str ."</pre>\n<br />";
292 $message_prepped .= "\n\t&raquo; <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->error_no . "</span>\n<br />";
293 $message_prepped .= "\n\t&raquo; <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->error_str . "</span>\n<br />";
294 $message_prepped .= "\n\t&raquo; <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
295 $message_prepped .= "\n\t&raquo; <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
296 $message_prepped .= "\n</p>\n</blockquote>";
297
298 $this->registry->_message('Database Error in `<em>' . $this->registry->application . '</em>`', $message_prepped, 3);
299 exit;
300 }
301 }
302 }
303
304 /*=====================================================================*\
305 || ###################################################################
306 || # $HeadURL$
307 || # $Id$
308 || ###################################################################
309 \*=====================================================================*/
310 ?>