2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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.
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
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 \*=====================================================================*/
23 * MySQL Database Abstraction Layer
30 * MySQL Database Abstraction Layer
32 * This framework is a function wrapper for MySQL functions so we can have
33 * better error reporting and query reporting.
35 * @author Iris Studios, Inc.
36 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
44 * Framework registry object
50 * Database that is used in the application
56 * Determines whether or not errors should be shown
62 * Current error number
68 * Description of current error
74 * Currend open MySQL connexion
86 * Current query string
92 * History of all executed queryies
95 var $history = array();
97 // ###################################################################
101 function __construct(&$registry)
103 $this->registry
=& $registry;
106 // ###################################################################
108 * (PHP 4) Constructor
110 function DB_MySQL(&$registry)
112 $this->__construct($registry);
115 // ###################################################################
117 * Connect to a the specified database
121 * @param string Server name
122 * @param string User name
123 * @param string Password
124 * @param bool Use p-connect?
126 * @return bool Result of connect
128 function connect($server, $user, $password, $pconnect)
130 if ($this->link_id
== 0)
134 $this->link_id
= @mysql_pconnect($server, $user, $password);
138 $this->link_id
= @mysql_connect($server, $user, $password);
141 if ($this->link_id
== false
)
143 $this->error('Link-Id == false, cannot connect');
147 if (@mysql_select_db($this->database
, $this->link_id
))
153 $this->error('Cannot use the database \'' . $this->database
. '\'');
159 // ###################################################################
161 * Send a query to MySQL
165 * @param string Query string
167 * @return integer Resource ID for query
169 function query($query_str)
171 $this->query_id
= @mysql_query($query_str, $this->link_id
);
172 $this->query_str
= $query_str;
173 $this->history
[] = $this->query_str
;
175 if (defined('ISSO_SHOW_QUERIES_LIVE'))
177 if (constant('ISSO_SHOW_QUERIES_LIVE'))
179 print($this->query_str
. '<hr />');
183 if (!$this->query_id
)
185 $this->error('Invalid SQL query');
188 return $this->query_id
;
191 // ###################################################################
193 * Escape a string depending on character set
197 * @param string String to be escaped
199 * @return string Escaped string
201 function escape_string($string)
203 return @mysql_real_escape_string($string, $this->link_id
);
206 // ###################################################################
208 * Fetch the query result as an array
212 * @param integer Query ID
214 * @return array A row of the query result
216 function fetch_array($query_id)
218 return @mysql_fetch_array($query_id, MYSQL_ASSOC
);
221 // ###################################################################
223 * Fetch the query result as an object
227 * @param integer Query ID
229 * @return object An object with the query result
231 function fetch_object($query_id)
233 return @mysql_fetch_object($query_id);
236 // ###################################################################
238 * Free the current MySQL query result
242 * @param integer Query ID
244 function free_result($query_id)
246 @mysql_free_result($query_id);
248 $this->query_str
= '';
251 // ###################################################################
253 * Send a MySQL query and return the first row of the results
257 * @param string Query string
258 * @param string Result return function (in the database layer)
260 * @return mixed Results in variable formats
262 function query_first($query_str, $callback = 'fetch_array')
264 $resource = $this->query($query_str);
265 $return = $this->$callback($resource);
266 $this->free_result($resource);
270 // ###################################################################
272 * Fetch the unique ID of the record just inserted
276 * @return integer MySQL insert ID
280 return @mysql_insert_id($this->link_id
);
283 // ###################################################################
285 * Fetch the number of rows in the result
289 * @param integer Query ID
291 * @return integer Number of MySQL rows
293 function num_rows($query_id)
295 return @mysql_num_rows($query_id);
298 // ###################################################################
300 * Fetch the number of rows affected by the query
304 * @param integer Query ID
306 * @return integer Number of affected rows
308 function affected_rows($query_id)
310 return @mysql_affected_rows($this->link_id
);
313 // ###################################################################
315 * MySQL error wrapper for ISSO::_message()
319 * @param string User defined error message
321 function error($message)
327 $this->error_no
= mysql_errno($this->link_id
);
328 $this->error_str
= mysql_error($this->link_id
);
331 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
333 $message_prepped = "<blockquote>\n<p>";
334 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->query_str
) ."</pre>\n<br />";
335 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->error_no
. "</span>\n<br />";
336 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->error_str
. "</span>\n<br />";
337 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
338 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
339 $message_prepped .= "\n</p>\n</blockquote>";
341 $this->registry
->message('Database Error in `<em>' . $this->registry
->application
. '</em>`', $message_prepped, 3);
347 /*=====================================================================*\
348 || ###################################################################
351 || ###################################################################
352 \*=====================================================================*/