]>
src.bluestatic.org Git - isso.git/blob - db_mysql.php
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();
100 function DB_MySQL(&$registry)
102 $this->registry
=& $registry;
106 * Connect to a the specified database
108 * @param string Server name
109 * @param string User name
110 * @param string Password
111 * @param bool Use p-connect?
113 * @return bool Result of connect
115 function connect($server, $user, $password, $pconnect)
117 if ($this->link_id
== 0)
121 $this->link_id
= @mysql_pconnect($server, $user, $password);
125 $this->link_id
= @mysql_connect($server, $user, $password);
128 if ($this->link_id
== false)
130 $this->error('Link-Id == false, cannot connect');
134 if (@mysql_select_db($this->database
, $this->link_id
))
140 $this->error('Cannot use the database \'' . $this->database
. '\'');
147 * Send a query to MySQL
149 * @param string Query string
151 * @return integer Resource ID for query
153 function query($query_str)
155 $this->query_id
= @mysql_query($query_str, $this->link_id
);
156 $this->query_str
= $query_str;
157 $this->history
[] = $this->query_str
;
159 if (defined('ISSO_SHOW_QUERIES_LIVE'))
161 if (constant('ISSO_SHOW_QUERIES_LIVE'))
163 print($this->query_str
. '<hr />');
167 if (!$this->query_id
)
169 $this->error('Invalid SQL query');
172 return $this->query_id
;
176 * Escape a string depending on character set
178 * @param string String to be escaped
180 * @return string Escaped string
182 function escape_string($string)
184 return @mysql_real_escape_string($string, $this->link_id
);
188 * Fetch the query result as an array
190 * @param integer Query ID
192 * @return array A row of the query result
194 function fetch_array($query_id)
196 return @mysql_fetch_array($query_id, MYSQL_ASSOC
);
200 * Fetch the query result as an object
202 * @param integer Query ID
204 * @return object An object with the query result
206 function fetch_object($query_id)
208 return @mysql_fetch_object($query_id);
212 * Free the current MySQL query result
214 * @param integer Query ID
216 function free_result($query_id)
218 @mysql_free_result($query_id);
220 $this->query_str
= '';
224 * Send a MySQL query and return the first row of the results
226 * @param string Query string
227 * @param string Result return function (in the database layer)
229 * @return mixed Results in variable formats
231 function query_first($query_str, $callback = 'fetch_array')
233 $resource = $this->query($query_str);
234 $return = $this->$callback($resource);
235 $this->free_result($resource);
240 * Fetch the unique ID of the record just inserted
242 * @return integer MySQL insert ID
246 return @mysql_insert_id($this->link_id
);
250 * Fetch the number of rows in the result
252 * @param integer Query ID
254 * @return integer Number of MySQL rows
256 function num_rows($query_id)
258 return @mysql_num_rows($query_id);
262 * Fetch the number of rows affected by the query
264 * @param integer Query ID
266 * @return integer Number of affected rows
268 function affected_rows($query_id)
270 return @mysql_affected_rows($this->link_id
);
274 * MySQL error wrapper for ISSO::_message()
276 * @param string User defined error message
278 function error($message)
284 $this->error_no
= mysql_errno($this->link_id
);
285 $this->error_str
= mysql_error($this->link_id
);
288 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
290 $message_prepped = "<blockquote>\n<p>";
291 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->query_str
) ."</pre>\n<br />";
292 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->error_no
. "</span>\n<br />";
293 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->error_str
. "</span>\n<br />";
294 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
295 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
296 $message_prepped .= "\n</p>\n</blockquote>";
298 $this->registry
->_message('Database Error in `<em>' . $this->registry
->application
. '</em>`', $message_prepped, 3);
304 /*=====================================================================*\
305 || ###################################################################
308 || ###################################################################
309 \*=====================================================================*/