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