]>
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 __construct(&$registry)
102 $this->registry
=& $registry;
106 * (PHP 4) Constructor
108 function DB_MySQL(&$registry)
110 $this->__construct($registry);
114 * Connect to a the specified database
118 * @param string Server name
119 * @param string User name
120 * @param string Password
121 * @param bool Use p-connect?
123 * @return bool Result of connect
125 function connect($server, $user, $password, $pconnect)
127 if ($this->link_id
== 0)
131 $this->link_id
= @mysql_pconnect($server, $user, $password);
135 $this->link_id
= @mysql_connect($server, $user, $password);
138 if ($this->link_id
== false)
140 $this->error('Link-Id == false, cannot connect');
144 if (@mysql_select_db($this->database
, $this->link_id
))
150 $this->error('Cannot use the database \'' . $this->database
. '\'');
157 * Send a query to MySQL
161 * @param string Query string
163 * @return integer Resource ID for query
165 function query($query_str)
167 $this->query_id
= @mysql_query($query_str, $this->link_id
);
168 $this->query_str
= $query_str;
169 $this->history
[] = $this->query_str
;
171 if (defined('ISSO_SHOW_QUERIES_LIVE'))
173 if (constant('ISSO_SHOW_QUERIES_LIVE'))
175 print($this->query_str
. '<hr />');
179 if (!$this->query_id
)
181 $this->error('Invalid SQL query');
184 return $this->query_id
;
188 * Escape a string depending on character set
192 * @param string String to be escaped
194 * @return string Escaped string
196 function escape_string($string)
198 return @mysql_real_escape_string($string, $this->link_id
);
202 * Fetch the query result as an array
206 * @param integer Query ID
208 * @return array A row of the query result
210 function fetch_array($query_id)
212 return @mysql_fetch_array($query_id, MYSQL_ASSOC
);
216 * Fetch the query result as an object
220 * @param integer Query ID
222 * @return object An object with the query result
224 function fetch_object($query_id)
226 return @mysql_fetch_object($query_id);
230 * Free the current MySQL query result
234 * @param integer Query ID
236 function free_result($query_id)
238 @mysql_free_result($query_id);
240 $this->query_str
= '';
244 * Send a MySQL query and return the first row of the results
248 * @param string Query string
249 * @param string Result return function (in the database layer)
251 * @return mixed Results in variable formats
253 function query_first($query_str, $callback = 'fetch_array')
255 $resource = $this->query($query_str);
256 $return = $this->$callback($resource);
257 $this->free_result($resource);
262 * Fetch the unique ID of the record just inserted
266 * @return integer MySQL insert ID
270 return @mysql_insert_id($this->link_id
);
274 * Fetch the number of rows in the result
278 * @param integer Query ID
280 * @return integer Number of MySQL rows
282 function num_rows($query_id)
284 return @mysql_num_rows($query_id);
288 * Fetch the number of rows affected by the query
292 * @param integer Query ID
294 * @return integer Number of affected rows
296 function affected_rows($query_id)
298 return @mysql_affected_rows($this->link_id
);
302 * MySQL error wrapper for ISSO::_message()
306 * @param string User defined error message
308 function error($message)
314 $this->error_no
= mysql_errno($this->link_id
);
315 $this->error_str
= mysql_error($this->link_id
);
318 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
320 $message_prepped = "<blockquote>\n<p>";
321 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->query_str
) ."</pre>\n<br />";
322 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->error_no
. "</span>\n<br />";
323 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->error_str
. "</span>\n<br />";
324 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
325 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
326 $message_prepped .= "\n</p>\n</blockquote>";
328 $this->registry
->message('Database Error in `<em>' . $this->registry
->application
. '</em>`', $message_prepped, 3);
334 /*=====================================================================*\
335 || ###################################################################
338 || ###################################################################
339 \*=====================================================================*/